-->





Translate

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

Leave a Comment
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.

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.

Concept Used
C++ Code :
If you enter more that one character program will show the result on the basis of first charater




#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>=&& storeAscii>=47
      || storeAscii>=54 && storeAscii<=64
      || storeAscii>=91 && storeAscii<=96 
      || storeAscii>=123 && storeAscii<=127)
  {
    cout<<"\nYou have entered a special character";
  }

return 0;
}

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



This Program helps to understand the operators like OR operator(||) how if else works, and ASCII values of characters

0 comments:

Post a Comment