In this tutorial, you will learn how to write a C program to check whether a character entered by user is an alphabet or not.
Example: Program to check whether a character is an Alphabet or not
In the following example, user is asked to enter a character, which is stored in a char
variable ch
. Once the value of entered character is captured using scanf() function, you can easily check whether its an alphabet or not by doing the following check in if statement.
For lowercase alphabets: >= ‘a’ and <= ‘z’
For uppercase alphabets: >= ‘A’ and <= ‘Z’
If the input character lies in one of these ranges then the character is an alphabet else it’s not an 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("Entered character %c is an alphabet.", ch);
else
printf("Entered character %c is not an alphabet.", ch);
return 0;
}
Output:
When the entered character is ‘A’
When the entered character is 9
You can also check the alphabet using the ASCII values of characters like this:
if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
printf("The entered character %c is an Alphabet",ch);
else
printf("The entered character %c is not an Alphabet",ch);
The ASCII value of ‘a’ is 97, ‘z’ is 122, ‘A’ is 65 and ‘Z’ is 90.
Leave a Reply