In this program, we are going to see whether a user given character is an alphabet or not. To determine the type of character, we have to check it’s ASCII value range.
See also: Find ASCII value of a character
Logic
To check if it is an alphabet or not we only need to check its ASCII value range. If it is an alphabet then it’s ASCII value range should be within 65 (A) – 90 (Z) or 97 (a) – 122 (z).
Program
#include <iostream> using namespace std; int main() { char ch; cout << "Enter any character: "; cin >> ch; if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { cout << endl << ch << " is ALPHABET."; } else { cout << endl<< ch << " is NOT ALPHABET."; } return 0; }
Output
Enter any character : x
x is ALPHABET.