Check character entry as digit, uppercase, lowercase, special – CSKC
Sign In

Search

Code Learning #CPP#1931    siteicon   siteicon  

Check character entry as digit, uppercase, lowercase, special

Finding if the character entered by the user is a digit, upper case alphabet, lower case alphabet or other special character using if-else-if ladder.

Learning Objectives

  • Using character values or ascii codes as range based checking in an if – else – if ladder construct for digits, upper case alphabets, lower case alphabets and all other special character.

Source Code

TC++ #1931

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

if(ch >= ‘0’ && ch <= ‘9’) cout << “You entered a digit ” ;
This is check range for a digit.

else if(ch >= ‘A’ && ch <= ‘Z’) cout << “You entered an upper case character ” ;
This is check range for upper case alphabet.

else if(ch >= 97 && ch <= 122) cout << “You entered a lower case character ” ;
This is check range for lower case alphabet given as ascii codes. you can also give this as (ch >= ‘a’ && ch <= ‘z’).

else cout << “You entered a special character ” ;
everything else has been treated as a special character.

 

Notes

  • Ranges in this program are not contiguous but they are mutually exclusive.
  • Automatic character to ascii comparison is possible in c++.

Common Errors

  • Some students miss the comparison of 0 while declaring it as digits.
  • comparison to ascii values is correctly possible only when correctly remembered ascii codes are put.
  • Sometimes students try to use int data type for this type of comparison. This works only when entry is given as ascii codes.


Suggested Filename(s): digit-lower-upper-special.cpp, checkchar.cpp



Share

Created: 27-Dec-2017 03:08 | Updated: 1-Sep-2026 16:40|