C++ Character Functions

For using character functions we need to include header file ctype.h

isalpha()


  • It stands for is alphabet
  • This function is used to check whether the character variable is alphabet or not.
  • If it contains an alphabet then function with return non zero value


Syntax


isalpha(charvar);


Program to show use of isalpha() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(isalpha(ch))
     {
         cout<<"\n It is an alphabet";
     }
     else
     {
         cout<<"\n It is not an alphabet";
     }
     getch();
}


Output

Enter any character = R
It is an alphabet

isdigit()


  • It stands for is digit
  • This function is used to check whether the character variable is digit or not.
  • If it contains digit then function with return non zero value


Syntax


isdigit(charvar);


Program to show use of isdigit() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(isdigit(ch))
     {
         cout<<"\n It is a digit";
     }
     else
     {
         cout<<"\n It is not a digit";
     }
     getch();
}


Output

Enter any character = 7
It is a digit

isalnum()


  • It stands for is alpha numeric
  • This function is used to check whether the character variable is digit or alphabet.
  • If it contains digit or alphabet then function with return non zero value


Syntax


isalnum(charvar);


Program to show use of isalnum() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(isalnum(ch))
     {
         cout<<"\n It is a digit or an alphabet";
     }
     else
     {
         cout<<"\n It is not a digit or an alphabet";
     }
     getch();
}


Output

Enter any character = 7
It is a digit or an alphabet

ispunct()


  • It stands for is punctuation.
  • This function is used to check whether the character variable is punctuation or not.
  • If it contains punctuation( #, $, %, ^, &, *, !, ;, etc.) then function with return non zero value


Syntax


ispunct(charvar);


Program to show use of ispunct() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(ispunct(ch))
     {
         cout<<"\n It is a punctuation";
     }
     else
     {
         cout<<"\n It is not a punctuation";
     }
     getch();
}


Output

Enter any character = &
It is a punctuation

isspace()


  • It stands for is space.
  • This function is used to check whether the character variable is blank space or not.
  • If it contains blank space then function with return non zero value


Syntax


isspace(charvar);


Program to show use of isspace() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(isspace(ch))
     {
         cout<<"\n It is a Blank Space";
     }
     else
     {
         cout<<"\n It is not a Blank Space";
     }
     getch();
}


Output

Enter any character = [Space Pressed]
It is a Blank Space

isupper()


  • It stands for is upper.
  • This function is used to check whether the character variable is alphabet in capital or not.
  • If it contains alphabet in capital then function with return non zero value


Syntax


isupper(charvar);


Program to show use of isupper() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(isupper(ch))
     {
         cout<<"\n It is a capital alphabet";
     }
     else
     {
         cout<<"\n It is not a capital alphabet";
     }
     getch();
}


Output

Enter any character = A
It is a capital alphabet

islower()


  • It stands for is lower.
  • This function is used to check whether the character variable is alphabet in lower case or not.
  • If it contains alphabet in lower case then function with return non zero value


Syntax


islower(charvar);


Program to show use of islower() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character = ";
     cin>>ch;
     if(islower(ch))
     {
         cout<<"\n It is a lower case alphabet";
     }
     else
     {
         cout<<"\n It is not a lower case alphabet";
     }
     getch();
}


Output

Enter any character = a
It is a lower case alphabet

toupper()


  • It stands for to upper.
  • This function is used to convert an alphabet in lower case to capital.


Syntax


toupper(charvar);


Program to show use of toupper() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character in lower case= ";
     cin>>ch;
     ch=toupper(ch);
     cout<<"\n toupper(ch)= "<<ch;  
     getch();
}


Output

Enter any character in lower case= a
 toupper(ch)= A

tolower()


  • It stands for to lower.
  • This function is used to convert an alphabet in capital to lower case.


Syntax


tolower(charvar);


Program to show use of toupper() function.



#include<ctype.h>
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch;
     cout<<"\n Enter any character in Capital= ";
     cin>>ch;
     ch=tolower(ch);
     cout<<"\n tolower(ch)= "<<ch;  
     getch();
}


Output

Enter any character in Capital= A
 tolower(ch)= a

Back To Top