Character functions in C

You must first complete Introduction to characters in C before viewing this Lesson



Character functions in C

Character functions need ctype.h  header file to be included in the pgoram. Different character functions provided by C Language are:

1.  isalpha():

This function checks whether the character variable/constant contains alphabet or not.  

2. isdigit()

This function checks whether the character variable/ constant contains digit or not.

3. isalnum()

This function checks whether the character variable/constant contains an alphabet or digit.

4. ispunct()

This function checks whether the character variable/constant contains a punctuator or not. Punctuators are comma, semicolon etc.

5.  isspace()

This function checks whether the character variable/constant contains a space or not. 

Program to demonstrate the use of character functions.
#include<ctype.h>
#include<stdio.h>
int main()
{
char n;
printf("\nEnter a character=");
n=getche();
if(isalpha(n)) 
printf("\nYou typed an alphabet");
if(isdigit(n)) 
printf("\nYou typed a digit");
if(isalnum(n)) 
printf("\nYou typed an alphabet or digit");
if(isspace(n)) 
printf("\nYou typed a blank space");
if(ispunct(n)) 
printf("\nYou typed punctuator");
return(0);
}
Output
Enter a character=A
You typed an alphabet
6. isupper() 

This function checks whether the character variable/constant contains an capital letter  alphabet or not. 

7.  islower()

This function checks whether the character variable/constant contains a lowercase alphabet or not.

8. toupper()

This function converts lowercase alphabet into uppercase alphabet.


9. tolower()

This function converts an uppercase alphabet into lowercase alphabet.

Program to use of islower,isupper,tolower(),toupper().
#include<ctype.h>
#include<stdio.h>
int main()
{
char n;
printf("\nEnter an alphabet=");
n=getche();
if(islower(n))
n=toupper(ch);
else
ch=tolower(ch);
printf("\nNow alphabet=%c",n);
return(0);
}
Output
Enter an alphabet=A
Now alphabet=a





Spread the love
Lesson tags: character formatting functions in c, character functions in c, check whether a character is capital, check whether a character is digit, passing character variable as function argument
Back to: C Programming Language
Spread the love