Character functions in C Language | isalpha(), isdigit(), isalnum(), ispunct(), isspace(), isupper(), islower(), toupper(), tolower()
Character functions need ctype.h header file to be included in the pgoram. Different character functions provided by C Language are:
– isalpha()
– isdigit()
– isalnum()
– ispunct()
– isspace()
– isupper()
– islower()
– toupper()
– tolower()
1 isalpha() function in C Language:
It stands for is-alphabet. This function is used to check whether the character variable or character constant contains an alphabet or not. If it contains an alphabet it will return a non zero value.
Syntax:
isalpha(var_name);
var_name specifies character variable or character constant which want to check for whether it contains an alphabet or not.
2 isdigit() function in C Language:
It stands for is-digit. This function is used to check whether the character variable or character constant contains a digit or not. If it contains a digit it will return a non zero value.
Syntax :
isdigit(var_name);
var_name specifies character variable or character constant which want to check for whether it contains a digit or not.
3 isalnum() function in C Language:
It stands for is-alphanumeric. This function is used to check whether the character variable or character constant contains an alphabet or digit. If it contains an alphabet or digit it will return a non zero value.
Syntax :
isalnum(var_name);
var_name specifies character variable or character constant which want to check for whether it contains an alphabet or a digit.
4. ispunct() function in C Language:
It stands for is-punctuator. This function is used to check whether the character variable or character constant contains a punctuator or not. Punctuators include comma, semicolon etc. If it contains a punctuator, it will return a non zero value.
Syntax:
ispunct(var_name);
var_name specifies character variable or character constant which want to check for whether it contains a punctuator or not.
5. isspace() function in C Language:
It stands for is-space. This function is used to check whether the character variable or character constant contains a blank space or not. If it contains a blank space, it will return a non zero value.
Syntax :
isspace(var_name);
var_name specifies character variable or character constant which want to check for whether it contains a blank space or not.
Program to demonstrate the use of character() function.
#include<ctype.h> #include<stdio.h> int main() { char ch; printf("\nEnter any character="); ch=getche(); if(isalpha(ch)) printf("\nYou typed an alphabet"); if(isdigit(ch)) printf("\nYou typed a digit"); if(isalnum(ch)) printf("\nYou typed an alphabet or digit"); if(ispunct(ch)) printf("\nYou typed a punctuator"); if(isspace(ch)) printf("\nYou typed a space"); return 0; }
Output
Enter any character= <blank Space> You typed a space
6 isupper() function in C Language:
It stands for is-upper. This function is used to check whether the character variable or character constant contains an uppercase alphabet or not. If it contains an uppercase alphabet, it will return a non zero value.
Syntax :
isupper(var_name);
var_name specifies character variable or character constant which want to check for whether it contains an uppercase alphabet or not.
7. islower() function in C Language:
It stands for is-lower. This function is used to check whether the character variable or character constant contains a lowercase alphabet or not. If it contains a lowercase alphabet, it will return a non-zero value.
Syntax :
islower(var_name);
var_name specifies character variable or character constant which want to check for whether it contains a lowercase alphabet or not.
Program to demonstrate the use of isupper() and islower() functions.
#include<ctype.h> #include<stdio.h> int main() { char ch; printf("\nEnter an alphabet="); ch=getche(); if(isupper(ch)) printf("\nYou typed an upper case alphabet"); if(islower(ch)) printf("\nYou typed a lower case alphabet"); return(0); }
Output
Enter an alphabet= a You typed a lower case alphabet
8. toupper() function in C Language:
It stands for to-uppercase. This function is used to convert a lowercase alphabet into uppercase alphabet. The alphabet may be in the form of a character variable or character constant.
Syntax :
toupper(var_name);
var_name specifies character variable or character constant which want to convert into uppercase.
9. tolower() function in C Language:
It stands for to-lowercase. This function is used to convert an uppercase alphabet into lowercase alphabet. The alphabet may be in the form of a character variable or character constant.
Syntax :
tolower(var_name);
var_name specifies character variable or character constant which want to convert into lowercase.
Program to demonstrate the use of toupper() and tolower() function.
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
int main()
{
char ch;
printf(“\nEnter an alphabet=”);
ch=getche();
ch=toupper(ch);
printf(“\nAlphabet in uppercase=%c”,ch);
ch=tolower(ch);
printf(“\nAlphabet in lowercase=%c”,ch);
return(0);
}
Output
Enter an alphabet= a Alphabet in uppercase=A Alphabet in lowercase=a