C++ Program to check whether a character is uppercase, lowercase ,digit or special character

Character is uppercase ,lowercase ,digit or special character

Here we will discuss how the check whether a character is uppercase, lowercase ,digit or special character in c++ programming language.

In C++ programming language a char type variable can store many different types of values ,128 values in total, single value at a time. Each value has been assigned a code by which they can be identified and that is called ASCII code.

  • Alphabets
    • Uppercase( between 65 to 90 )
    • Lowercase( between 97 to 122)
  • Digits (between 48 to 57)
  • Special Characters (all the remaining in between 0 to 127)
Uppercase Lowercase Digit or Special Character

Working:

  • User gives an input.
  • The input is stored in a char type variable say prep.
  • prep is then checked using the if else-if statement.
  • For uppercase alphabets

                       if((prep >= 65) && (prep <= 90)) 

  • For lowercase alphabets

                       else if((prep >= 97) && (prep <= 122))

  • For digits

                       else if((prep >= 48) && (prep <= 57))

  • All others will be the special characters.

C++ code

//C++ program
//Character is alphabet, a digit, or a special character
#include<iostream>
using namespace std;
int main()
{
char charCheck;
cout<<"Enter a character: ";
cin>>charCheck;
//for uppercase alphabets
if((charCheck >= 65) && (charCheck <= 90))
{
cout<<charCheck<<" is Uppercase Alphabet";
}
// for lowercase alphabets
else if((charCheck >= 97) && (charCheck <= 122))
{
cout<<charCheck<<" is Lowercase Alphabet";
}
//for digits
else if((charCheck >= 48) && (charCheck <= 57))
{
cout<<charCheck<<" is a Digit";
}
//all remaining are special characters
else
{
cout<<charCheck<<" is a Special Character";
}
return 0;
}

Output:

Enter a character: @
@ is a Special Character