C++ Program to Check Whether a Character is an Alphabet or Not

In this post, we will learn how to check whether a character is an alphabet or not using C++ Programming language.

In C++ programming language, every character holds an ASCII value for computer usage. The ASCII values of lowercase alphabets are from 97 to 122 and the ASCII values of uppercase alphabets are from 65 to 90.

We will check for both lowercase as well as uppercase alphabets. The below program asks the user to enter a character.

If the entered character is an alphabet, then we display the message that the entered character is an alphabet, otherwise we display that the entered character is not an alphabet.

So, without further ado, let’s begin this 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 <iostream>
using namespace std;

int main(){
    char ch;
    
    // Asking for input 
    cout << "Enter a character: ";
    cin >> ch;
    
    // Checking for alphabet
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        cout << ch << " is an alphabet.";
    }
    else {
        cout << ch << " is not an alphabet.";
    }
    return 0;
}

Output 1

Enter a character: S
S is an alphabet.

Output 2

Enter a character: @
@ is not an alphabet.

How Does This Program Work?

    char ch;

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

    // Asking for input 
    cout << "Enter a character: ";
    cin >> ch;

The user is asked to enter a character. The ch named variable holds the value of the entered character.

    // Checking for alphabet
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        cout << ch << " is an alphabet.";
    }
    else {
        cout << ch << " is not an alphabet.";
    }

Now, we use an if-else statement to check whether the entered character is an alphabet or not.

  • If ch >= ‘a’ and ch <= ‘z’, then the entered character is an alphabet.

Similarly, we check for the uppercase letters.

  • If ch >= ‘A’ and ch <= ‘Z’, then the entered character is an alphabet.

C++ Program to Check Whether a Character is an Alphabet or Not Using ASCII Value

// C++ Program to Check Whether a Character is an Alphabet or Not Using ASCII Values
#include <iostream>
using namespace std;

int main(){
    char ch;
    
    // Asking for input
    cout << "Enter a character: ";
    cin >> ch;
    
    // Checking Using ASCII Value
    if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)){
        cout << ch << " is an alphabet.";
    }
    else {
        cout << ch << " is not an alphabet.";
    }
    return 0;
}

Output 1

Enter a character: C
C is an alphabet.

Output 2

Enter a character: !
! is not an alphabet.

Conclusion

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

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published.