C++ program to check the entered character is capital letter, small letter, digit or a special character


Levels of difficulty: / perform operation:

C++ program which takes input a character and check it whether entered character is capital letter, small letter, Digit or Special character

All characters like small letters, capital letters, digits or special character have ASCII codes when a key is pressed from keyboard there is always an ASCII value behind it like if small letter ‘a’ is pressed its ASCII is 97 if capital letter ‘A’ is pressed its ASCII value is 65 if a number digit ‘0’ is pressed its ASCII value is 48.


For ASCII value ranges
0 – 9 48 – 57
A – Z 65 – 90
a – z 97 – 122

Special Characters 0-47, 58-64, 91-96, 123-127

On the basis of ASCII values and using operators like and operator (&&) or Operator (||) we can differentiate the letters.

C++ Program

#include<iostream>
using namespace std;
int main() {
	char character;
	cout<<"Enter a character =  ";
	cin>>character;
	int storeAscii=character;
	cout<<"The ASCII value  of "<<character<<
	        " is "<<storeAscii;
	if (storeAscii>=65 && storeAscii<=90) {
		cout<<"\nYou have entered a capital letter";
	} else if (storeAscii>=97 && storeAscii<=122) {
		cout<<"\nYou have entered a small letter";
	} else if (storeAscii>=47 && storeAscii<=57) {
		cout<<"\nYou have entered a digit ";
	} else if (storeAscii>=0 && storeAscii>=47
	      || storeAscii>=54 && storeAscii<=64
	      || storeAscii>=91 && storeAscii<=96 
	      || storeAscii>=123 && storeAscii<=127) {
		cout<<"\nYou have entered a special character";
	}
	return 0;
}