C program for testing the character type



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 −

Function Checks whether entered character is
isalpha An alphabet (or) not
isdigit A digit (or) not
isspace Q A space, a newline (or) tab
ispunct ( A special symbol (or) not
islower A lower case letter of alphabet
isupper Q An upper case letter of alphabet
isalphanumeric An alphabet/digit or not

Converting Functions

The converting functions are listed below −

Function Conversion
tolower() Converts an upper case alphabet to lower case
toupper Q Converts 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
");    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.
Updated on: 2021-03-26T07:33:20+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements