How to Write a C Program to Check Whether the Character is Alphabet, Digit, or a Special Character with an example. For this, we are going to use the Built-in functions isalpha, isdigit, and ASCII Codes.
C Program to Check Character is Alphabet Digit or Special Character using else if
This C program allows the user to enter one character. Then, it will check whether the character is an Alphabet, digit, or Special Character.
In this example, we are going to use the Programming built-in functions isalpha and isdigit to find whether the character is an Alphabet or a Digit. If both conditions fail, it is a special char.
#include <stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf(" Please Enter any character : ");
scanf("%c", &ch);
if (isalpha(ch))
{
printf("\n %c is an Alphabet", ch);
}
else if (isdigit(ch))
{
printf("\n %c is a Digit", ch);
}
else
printf("\n %c is a Special Character", ch);
return 0;
}
Alphabet, Digit, or Special Character output.
Let us check whether 9 is an Alphabet or Digit or Special Character
Please Enter any character : 9
9 is a Digit
Let us check Special Character
Please Enter any character : @
@ is a Special Character
Program to Check Character is Alphabet Digit or Special Character Example 2
In this program, to check the Alphabet, digit, or special character, we use the alphabet and digits directly inside the Else If Statement.
#include <stdio.h>
int main()
{
char ch;
printf(" Please Enter any character : ");
scanf("%c", &ch);
if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
{
printf("\n %c is an Alphabet", ch);
}
else if (ch >= '0' && ch <= '9')
{
printf("\n %c is a Digit", ch);
}
else
printf("\n %c is a Special Character", ch);
return 0;
}
Check Character is an Alphabet, Digit, or Special Character using ASCII values
In this program, we are using the ASCII Table values to check whether the input character is an Alphabet, Digit, or special character.
#include <stdio.h>
int main()
{
char ch;
printf(" Please Enter any character : ");
scanf("%c", &ch);
if (ch >= 48 && ch <= 57)
{
printf("\n %c is a Digit", ch);
}
else if ( (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) )
{
printf("\n %c is an Alphabet", ch);
}
else
printf("\n %c is a Special Character", ch);
return 0;
}
Let us check whether 3 is a Digit or not
Please Enter any character : 3
3 is a Digit
Let us check whether $ is a special character or not
Please Enter any character : $
$ is a Special Character