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.

 

 

 

 

Published by 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.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *