Home > Tutorials > C Tutorials > Programming In C: Character Type Library Functions

Programming In C: Character Type Library Functions

Character Type Header File

If you need to check what type of character the viewer typed, use ctype.h which is the character type header file. We write it like this: #include<ctype.h>

Here is a list of character type library functions:

isalpha  checks if the input is an alphabetical letter 
isdigit  checks if the input is a number 
isupper  checks if the input is an upper case letter 
islower  checks if the input is an lower case letter 

Let’s examine each of these character type library functions.

isalpha: checks if the input is an alphabetical letter

To check whether the viewer types a letter or number, use isalpha. Here is the syntax:

  • if the input is a letter, then isalpha != 0
  • if the input is NOT a letter, then isalpha == 0

Here is an example:

#include<stdio.h>
#include<ctype.h>
void main( )
{
char letter;
printf(“Type a letter: “);
scanf(” %c”, &letter);
if(isalpha(letter) == 0)
printf(“You did NOT type a letter!”);
else
printf(“Your letter is %c.”, letter);
}
 

Type this source code in your editor and save it as alph.c then compile it, link it, and run it.

isdigit: checks if the input is a number

We can use isdigit the same way as isalpha. Here is the syntax:

  • if the input is a number, then isdigit != 0
  • if the input is NOT a number, then isdigit == 0

Here is an example:

#include<stdio.h>
#include<ctype.h>
void main( )
{
int year;
printf(“What year is it? “);
scanf(“%d”, &year);
if(isdigit(year) == 0)
printf(“You did NOT type a number!”);
else
printf(“The year is %d.”, year);
}
 

Type this source code in your editor and save it as dig.c then compile it, link it, and run it.

 

isupper: checks if the input is an upper case letter

If you want to check the case of a letter, use isupper.

  • if the input is in upper case, then isupper != 0
  • if the input is NOT in upper case, then isupper == 0

Here is an example:

#include<stdio.h>
#include<ctype.h>
void main( )
{
char first;
printf(“What is the first letter of your name? “);
scanf(” %c”, &first);
if(isupper(first) == 0)
printf(“You should capitalize the first letter of your name.”);else

printf(“%c is the first letter of your name.”, first);}

Type this source code in your editor and save it as up.c then compile it, link it, and run it.

islower: checks if the input is a lower case letter

This is the opposite of isupper.

 

 

 

 

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close