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
|
|
Run Output
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
Created: 27-Dec-2017 03:08 | Updated: 1-Sep-2026 16:40|
