In this example, you will learn to check a given character is alphabet or not.
Contents
In C language, every character has a unique ASCII value. The ASCII value of the uppercase alphabet lies between 65 and 90, the lowercase alphabet lies between 97 and 122.
The character input is taken from the user and compared with lower and uppercase of A and Z. In the comparison if the character lies between the range of either lower or uppercase of A and Z, then the character is an alphabet.
We can also check the character is an alphabet or not by using the predefined function isalpha() defined under ctype.h library.
C Program to Check Alphabet
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
printf("%c is an alphabet.", ch);
else
printf("%c is not an alphabet.", ch);
return 0;
}
Run CodeC Program to Check Alphabet using isalpha() function
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isalpha(ch))
printf("%c is an alphabet.", ch);
else
printf("%c is not an alphabet.", ch);
return 0;
}
Run CodeOutput
Enter a character: A
A is an alphabet.
Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.