codescracker
c

C Program to Check Alphabet or Not



« Previous Program Next Program »

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

Check Alphabet in C

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

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

// Program to Check Alphabet in C
// -------codescracker.com-------

#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 Code::Blocks IDE. Here is the sample run:

c program check alphabet or not

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

c program alphabet

As c lies between a-z. So it is an alphabet. Let's take a look at another sample run, where user enters any character that does not lies between a-z and A-Z say - (dash or minus, whatever you understand it). Here is the sample run:

alphabet c program

Check Alphabet using ASCII Value in C

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

Here is the ASCII table of uppercase alphabet:

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

And the ASCII table of lowercase alphabet:

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

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

// Program to Check Alphabet using ASCII code in C
// -------codescracker.com-------

#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 (contains Y) as its value, to an integer variable say ascii, then the ASCII value of ch (121, ASCII code of Y) gets initialized to ascii variable. So if ch holds A as its value, then using the statement ascii = ch;, 65 gets initialized to ascii. Therefore, use if-else case to check and print whether it is an alphabet or not.

You can also check it directly with ch variable itself without using any extra variable like ascii (used in above program). Here extra variable say ascii is only used to make it more simple to understand.

Same Program in Other Languages


« Previous Program Next Program »