Character functions in C

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



Character functions in C

There are various character functions provided by C Language to check or change the case of value of a character variable or a character constant. We need to include header file ctype.h in our program to use these functions in our program. Various character functions are:

  • isalpha()
  • isdigit()
  • isalnum()
  • ispunct()
  • isspace()
  • isupper()
  • islower()
  • iscntrl()
  • toupper()
  • tolower()

1.  isalpha():

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. The syntax for isalpha() is :

isalpha(var_name);

var_name specifies character variable or character constant which want to check for whether it contains an alphabet or not. 

 Program to demonstrate the use of isalpha() 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");
return(0);
}
Output
Enter any character=A
You typed an alphabet

2. isdigit()

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. The syntax for isdigit() is

isdigit(var_name); 

Program to demonstrate the use of isdigit() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter any character=");
ch=getche();
if(isdigit(ch))
printf("\nYou typed a digit");
return(0);
}
Output
Enter any character=9
You typed a digit

3. isalnum()

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. The syntax for isalnum() is

isalnum(var_name);

Program to demonstrate the use of isalnum() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter any character=");
ch=getche();
if(isalnum(ch))
printf("\nYou typed a digit or alphabet");
return(0);
}
Output
Enter any character=9
You typed a digit or alphabet



4. ispunct()

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. The syntax for ispunct() is

ispunct(var_name);

Program to demonstrate the use of ispunct() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter any character=");
ch=getche();
if(ispunct(ch))
printf("\nYou typed a punctuation character");
return(0);
}
Output
Enter any character=;
You typed a punctuation character

5.  isspace()

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. The syntax for isspace() is

isspace(var_name);

Program to demonstrate the use of isspace() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter any character=");
ch=getche();
if(isspace(ch))
printf("\nYou entered a space");
return(0);
}
Output
Enter any character=
You entered a space

6.  isupper()

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. The syntax for isupper() is

isupper(var_name);

Program to demonstrate the use of isupper() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter an alphabet=");
ch=getche();
if(isupper(ch))
printf("\nYou typed an uppercase alphabet");
return(0);
}
Output
Enter an alphabet=A
You typed an uppercase alphabet

7. islower()

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. The syntax for islower() is

islower(var_name); 

 Program to demonstrate the use of isdigit() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter an alphabet=");
ch=getche();
if(isupper(ch))
printf("\nYou typed a lowercase alphabet");
return(0);
}
Output
Enter an alphabet=a
You typed a lowecase alphabet



8. toupper()

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 of toupper() is

toupper(var_name);

Program to demonstrate the use of toupper() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter an alphabet in lowercase=");
ch=getche();
ch=toupper(ch);
printf("\nAlphabet in uppercase=%c",ch);
return(0);
}
Output
Enter an alphabet in lowercase= a
Alphabet in uppercase=A

9. tolower()

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 of tolower() is

tolower(var_name);

Program to demonstrate the use of tolower() function.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter an alphabet in capital=");
ch=getche();
ch=tolower(ch);
printf("\nAlphabet in lowercase=%c",ch);
return(0);
}
Output
Enter an alphabet in capital=A
Alphabet in lowercase=a

Passing character type data as function argument

We can pass a char type value as a function argument as we pass other types of values as arguments to a function.

Program to pass a character type variable as a function argument
#include<ctype.h>
#include<stdio.h>
void show(char n)
{
printf("Character is %c",n);
}
int main()
{
char ch='A';
show(ch);
return(0);
}
Output
Character is A



 TIPS and Tricks

  • h is a header file required for using character formatting functions like isupper(), islower(), toupper(), tolower() etc.
  • Character formatting functions are part of C library which help programmer to perform various character based operations with ease.

 Test Your Knowledge

1.  Write a program to read gender of a person. The value of gender must be ‘M’ or ‘F’.  An error message should be displayed if the user enters wrong value.

2. Write a program to assign grade as per following conditions:
If percentage is between 80 and 100 then
Grade=’A’
If percentage is between 60 and 80 then
Grade=’B’
If percentage is between 40 and 60 then
Grade=’C’
If percentage is between 0 and 40 then
Grade=’D’

3. Write the output of the following programs:
a)
int main( )
{
char ch=’A’;
printf(“%c”,ch);
ch+=3;
printf(“%c”,ch);
return(0);
}

b)
int main( )
{
char ch=’H’;
if(ch>=’A’ && ch<=’Z’)
ch-=32;
printf(“%c”,ch);
return(0);
}

c)
int main( )
{
char ch=97;
printf(“%c”,ch);
return(0);
}

d)
int main( )
{
char ch=97;
if(isalnum(ch)
printf(“%c”,ch);
return(0);
}



Best Books of C

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