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

In this post, you will learn how to check whether a Character is an Alphabet or Not using C Programming language.

This program will take the character value from the user and check whether it is an alphabet or not.

Uppercase Alphabets – A to Z

Lowercase Alphabets – a to z

Any character which lies in this range is an alphabet.

So, Without further ado, let’s begin the tutorial.

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

// C Program To Check Whether a Character is an Alphabet or not
#include <stdio.h>
int main(){
    char ch;
    
    // Asking for Input
    printf("Enter a Character: ");
    scanf("%c", &ch);
    
    // logic
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        printf("The entered Character %c is an Alphabet.", ch);
    }
    else {
        printf("The entered Character %c is not an Alphabet.", ch);
    }
    return 0;
}

Output 1

Enter a Character: N
The entered Character N is an Alphabet.

Output 2

Enter a Character: @
The entered Character @ is not an Alphabet.

How Does This Program Work ?

    char ch;

In this example, we have declared a character data type variable named as ch.

    // Asking for Input
    printf("Enter a Character: ");
    scanf("%c", &ch);

Then, the user is asked to enter the value of the character. This value will be stored in the ch variable.

    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        printf("The entered Character %c is an Alphabet.", ch);
    }

If the value of the character lies in the range, then our condition becomes True, and the program will print the message showing “The entered Character is an Alphabet.”

    else {
        printf("The entered Character %c is not an Alphabet.", ch);
    }

If the above condition is False, then the program will display “The entered character is not an Alphabet.”

Any character which lies in this range is an alphabet.

In C Programming language, every variable holds an ASCII Value. The ASCII Value of lowercase alphabets is from 97 to 122 and ASCII value of uppercase alphabets is from 65 to 90.

If the value of the character lies in this range, then the character will be an alphabet.

    // logic
    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);
    }

We can also use isalpha() function to check whether a character is an alphabet or not.

if (isalpha(ch)){
        printf("The entered %c character is an Alphabet.", ch);
    }
    else {
        printf("The entered %c character is not an Alphabet.", ch);
    }

Some used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.  

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen. 

|| – It is known as Logical OR Operator. If any of the two operations is non-zero, then the condition becomes true.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

isalpha() – isalpha() function checks whether a character is  an alphabet or not in C.

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you understand how to check whether a character is an alphabet or not in C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *