C Program to Check Whether a Character is an Alphabet or not

CodingTute

Updated on:

C Program to Check Alphabet

In this example, you will learn to check a given character is alphabet or not.

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 Code

C 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 Code

Output

Enter a character: A
A is an alphabet.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Exit mobile version