C program to check the alphabet or not

In this article, you will learn and get code for checking whether the character given by the user (at run-time) is an alphabet or not. There are two versions of the same program:

Check the alphabet in C

The question is: write a program in C to check whether the given input is alphabetic or not. The answer to this question is given below. This program uses the character itself (provided by the user at run-time) to check whether it is an alphabet or not.

All characters between a and z are alphabets (including a, z, and A, Z). So to check whether the input character is an alphabet or not in C programming, you have to ask the user to enter a character and then check whether it lies between a-z or A-Z or not. If it is, then it is an alphabet; otherwise, it is not an alphabet. Let's take a look at the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter a Character: ");
    scanf("%c", &ch);
    if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
        printf("\n%c is an Alphabet", ch);
    else
        printf("\n%c is not an Alphabet", ch);
    getch();
    return 0;
}

This program was compiled and executed using the Code::Blocks IDE. Here is the sample run:

c program check alphabet or not

Now supply any character as input, say "c," and press the ENTER key to see the output as shown in the snapshot given below:

c program alphabet

Because c is located between a and z. So it is an alphabet. Consider another sample run in which the user enters any character that does not fall between a-z and a-z, such as - (dash or minus). Here is the sample run:

alphabet c program

Check the alphabet using ASCII values in C

The question is, "Write a program in C that takes any character as input and uses the ASCII value of this character to check whether it is an alphabet or not."

Here is the ASCII table of the uppercase alphabet:

Character ASCII Value
A 65
B 66
C 67
... ...
Z 90

And the ASCII table of the lowercase alphabet

Character ASCII Value
a 97
b 98
c 99
... ...
z 122

Now use the above ASCII table to check for alphabets as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    int ascii;
    printf("Enter a Character: ");
    scanf("%c", &ch);
    ascii = ch;
    if((ascii>=97 && ascii<=122) || (ascii>=65 && ascii<=90))
        printf("\n%c is an Alphabet", ch);
    else
        printf("\n%c is not an Alphabet", ch);
    getch();
    return 0;
}

Here is the sample run of this program:

alphabet program c

As you can see from the above program, if you initialize any character, say ch (which contains Y) as its value, to an integer variable, say ascii, then the ASCII value of ch (121, the ASCII code of Y) gets initialized to the ascii variable. So if ch holds A as its value, then, using the statement ascii = ch;, 65 gets initialized to ascii. Therefore, use the if-else case to check and print whether it is an alphabet or not.

You can also check it directly with the ch variable itself without using any extra variable like ascii (used in the above program). An extra variable, such as ascii, is only used to make things clearer.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!