Here is a C++ program to check whether a character is alphabet or not. In this C++ program to check whether a character is Alphabet or not we will compare the ASCII value of given character with the range of ASCII values of alphabets. Here we will check for both uppercase as well as lowercase alphabets.
- The ASCII values of all uppercase characters are between 65 to 92. ('A' = 65 and 'Z' = 90).
- The ASCII values of all lowercase characters are between 67 to 122. ('a' = 97 and 'z' = 122).
How to check a character is alphabet or not
Let the given character is c. Here is the if else condition to determine alphabet characters.
if((c >= 'a'&& c <= 'z') || (c >= 'A' && c <= 'Z')) { cout << c << " is an Alphabet."; } else { cout << c << " is not an Alphabet."; }
C++ Program to check a character is alphabet or not
#include <iostream> using namespace std; int main() { char c; cout << "Enter a character\n"; cin >> c; if((c >= 'a'&& c <= 'z') || (c >= 'A' && c <= 'Z')) { cout << c << " is an Alphabet."; } else { cout << c << " is not an Alphabet."; } return 0; }Output
Enter a character C C is an Alphabet.
Enter a character g g is an Alphabet.
Enter a character 9 9 is not an Alphabet.
In above program, we first take a character input from user using cin and store it in variable c. Then we check whether c is alphabet or not using above mentioned if-else statement.
Recommended Posts