Sunday 5 March 2017

Functions of ctype.h header file in C/C++ Program


Character functions are defined in the ctype.h header file. These functions are used to manipulate the charters as defined. The important functions of ctype.h are isalnum(), isalpha(), isdigit(), islower(), isupper(),  tolower(), and toupper(). The use of these functions are given below:

isalnum()– to check alphanumeric(digit or letter)character, returns 1(true)or 0(false)
isalpha() – to check alphabet character, returns 1(true)or 0(false)
isdigit()– to check digit as character, returns 1(true)or 0(false)
islower() – to check lower alphabet character, returns 1(true)or 0(false)
isupper()– to upper alphabet character, returns 1(true)or 0(false)
tolower() – to convert ASCII of upper case alphabet to lower case
toupper()– to convert ASCII of lower case alphabet to upper case

To understand the use of above functions, let consider the given example:
 
Example program to show the use of character functions
 
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
 
cout<<"\n Enter any character from key board:\n";
cin>>ch;
 
if(isalnum(ch)) 
cout<<"\n it is an alphanumeric";
else
cout<<"\n it is an special character";
 
if(isdigit(ch))
cout<<"\n it is a digit";
if(isalpha(ch))
cout<<"\n it is an alphabet";
if(islower(ch))
{
cout<<"\n it is lower case alphabet";
cout<<"\n its upper case is :"<<(char)toupper(ch);
}
if(isupper(ch))
{
 cout<<"\n it is upper case alphabet";
 cout<<"\n its lower case is :"<<(char)tolower(ch);
 }
}
 
Output of Example program 
Enter any character from key board:
G
it is an alphanumeric
it is an alphabet
it is upper case alphabet
its lower case is :g


No comments:

Post a Comment