Q:-Write a C program to check a character is an alphabet, digit or special character.
In this section, we will see how to check whether a given character is number, or the alphabet or some special character in C.
The alphabets are from A – Z and a – z, Then the numbers are from 0 – 9. And all other characters are special characters. So If we check the conditions using these criteria, we can easily find them.
#include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf("\nEnter Any Character :"); scanf("%c",&ch); if(ch>='0' && ch<='9') { printf("\n Entered Character is Digit"); } else if(ch>='A' && ch<='Z') { printf("\n Entered Character is Capital Letter"); } else if(ch>='a' && ch<='z') { printf("\n Entered Character is Small Letter"); } else { printf("\n Entered Character is Special Character"); } getch(); }
Input a character from user. Store it in some variable say ch.
First check if character is alphabet or not. A character is alphabet if((ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)).
Next, check condition for digits. A character is digit if(ch >= ‘0’ && ch <= ‘9’).
Finally, if a character is neither alphabet nor digit, then character is a special character.