C program for testing the character type

CServer Side ProgrammingProgramming

There are some predefined functions available in "ctype.h" library for analyzing the character input and converting them.

Analysis Functions

The character analysis functions are listed below −

FunctionChecks whether entered character is
isalphaAn alphabet (or) not
isdigitA digit (or) not
isspace QA space, a newline (or) tab
ispunct (A special symbol (or) not
islowerA lower case letter of alphabet
isupper QAn upper case letter of alphabet
isalphanumericAn alphabet/digit or not

Converting Functions

The converting functions are listed below −

FunctionConversion
tolower()Converts an upper case alphabet to lower case
toupper QConverts a lower case alphabet to upper case

Program

Following is the C program for character analysis and conversion functions which are used to test the character type −

 Live Demo

#include <stdio.h>
#include <ctype.h>
main(){
   char character;
   printf("Press any key digit or alphabet\n");
   character = getchar();
   if (isalpha(character) > 0)
      printf("The character is a letter.");
   else
      if (isdigit (character) > 0)
         printf("The character is a digit.");
      else
   printf("The character is not alphanumeric.");
}

Output

When the above program is executed, it produces the following result −

Run 1:
Press any key digit or alphabet
3
The character is a digit.
Run 2:
Press any key digit or alphabet
G
The character is a letter.
Run 3:
Press any key digit or alphabet
&
The character is not alphanumeric.
raja
Published on 26-Mar-2021 12:02:15
Advertisements