Check whether the given character is in upper case or lower case or none
What is ASCII?
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numeric values (0-127) to characters.
Why ASCII matters for this problem:
When you write 'A'
or 'z'
in C++, the computer stores them as numbers!
'A'
is stored as 65'Z'
is stored as 90'a'
is stored as 97'z'
is stored as 122Complete ASCII character ranges:
Character Type | ASCII Range | Decimal Values | Total Count |
---|---|---|---|
Uppercase Letters | A-Z | 65-90 | 26 letters |
Lowercase Letters | a-z | 97-122 | 26 letters |
Digits | 0-9 | 48-57 | 10 digits |
Space | (space) | 32 | 1 character |
🔍 Key observation:
Complete uppercase ASCII table:
Char | ASCII | Char | ASCII | Char | ASCII |
---|---|---|---|---|---|
A | 65 | J | 74 | S | 83 |
B | 66 | K | 75 | T | 84 |
C | 67 | L | 76 | U | 85 |
D | 68 | M | 77 | V | 86 |
E | 69 | N | 78 | W | 87 |
F | 70 | O | 79 | X | 88 |
G | 71 | P | 80 | Y | 89 |
H | 72 | Q | 81 | Z | 90 |
I | 73 | R | 82 |
Complete lowercase ASCII table:
Char | ASCII | Char | ASCII | Char | ASCII |
---|---|---|---|---|---|
a | 97 | j | 106 | s | 115 |
b | 98 | k | 107 | t | 116 |
c | 99 | l | 108 | u | 117 |
d | 100 | m | 109 | v | 118 |
e | 101 | n | 110 | w | 119 |
f | 102 | o | 111 | x | 120 |
g | 103 | p | 112 | y | 121 |
h | 104 | q | 113 | z | 122 |
i | 105 | r | 114 |
Our condition: ch >= 'A' && ch <= 'Z'
What really happens behind the scenes:
M
ch
as: 77
(ASCII value)ch >= 'A'
becomes: 77 >= 65
→ TRUE ✅ch <= 'Z'
becomes: 77 <= 90
→ TRUE ✅TRUE && TRUE
→ TRUE ✅Breaking down ch >= 'A' && ch <= 'Z'
:
Part 1: ch >= 'A'
ch >= 65
Part 2: ch <= 'Z'
ch <= 90
Part 3: &&
(AND operator)
Visual representation:
ASCII: 0 ... 64 | 65...90 | 91...96 | 97...122 | 123...127
NONE | A...Z | NONE | a...z | NONE
| ✅ | | |
Why this works for ANY letter:
Input | ASCII | ch >= 'A' (65)? | ch <= 'Z' (90)? | Result |
---|---|---|---|---|
A | 65 | TRUE (65≥65) | TRUE (65≤90) | ✅ UPPERCASE |
M | 77 | TRUE (77≥65) | TRUE (77≤90) | ✅ UPPERCASE |
Z | 90 | TRUE (90≥65) | TRUE (90≤90) | ✅ UPPERCASE |
a | 97 | TRUE (97≥65) | FALSE (97>90) | ❌ Not uppercase |
5 | 53 | FALSE (53<65) | TRUE (53≤90) | ❌ Not uppercase |
💡 Key insight: We're not comparing characters - we're comparing their ASCII numbers!
Example 1: Uppercase letter (input = 'Z')
char ch;
cin >> ch;
→ User enters Z
ch = 'Z'
(internally: ch = 90
)ch >= 'A' && ch <= 'Z'
90 >= 65
→ TRUE ✅90 <= 90
→ TRUE ✅TRUE && TRUE
→ TRUE ✅cout << "UPPERCASE";
Example 2: Lowercase letter (input = 'z')
ch = 'z'
(internally: ch = 122
)ch >= 'A' && ch <= 'Z'
122 >= 65
→ TRUE ✅122 <= 90
→ FALSE ❌TRUE && FALSE
→ FALSE ❌ch >= 'a' && ch <= 'z'
122 >= 97
→ TRUE ✅122 <= 122
→ TRUE ✅TRUE && TRUE
→ TRUE ✅cout << "LOWERCASE";
Example 3: Digit (input = '5')
ch = '5'
(internally: ch = 53
)ch >= 'A' && ch <= 'Z'
53 >= 65
→ FALSE ❌ch >= 'a' && ch <= 'z'
53 >= 97
→ FALSE ❌cout << "NONE";
Example 4: Special character (input = '@')
ch = '@'
(internally: ch = 64
)64 >= 65
→ FALSE ❌64 >= 97
→ FALSE ❌Example 5: Space (input = ' ')
ch = ' '
(internally: ch = 32
)32 >= 65
→ FALSE ❌32 >= 97
→ FALSE ❌Question: Why didn't we use switch for this problem?
❌ Switch would be TERRIBLE here:
switch (ch) {
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
cout << "UPPERCASE";
break;
case 'a': case 'b': case 'c': // ... 26 more cases!
cout << "LOWERCASE";
break;
default:
cout << "NONE";
}
Problems with switch approach:
✅ If-else with range checking is perfect:
if (ch >= 'A' && ch <= 'Z')
cout << "UPPERCASE";
else if (ch >= 'a' && ch <= 'z')
cout << "LOWERCASE";
else
cout << "NONE";
Advantages:
When to use switch vs if-else:
Use Switch | Use If-Else |
---|---|
Discrete values (1, 2, 3, etc.) | Range checks (1-10, A-Z) |
Few specific cases (menu: 1-5) | Many values in a range |
Exact equality (day == 1) | Comparisons (>=, <=, >, <) |
Enums, day/month numbers | Complex conditions (&&, ||) |
💡 Rule of thumb: If you need >=
or <=
, use if-else. If you need ==
with specific values, consider switch!
Method 1: Our approach (manual range checking)
if (ch >= 'A' && ch <= 'Z')
cout << "UPPERCASE";
else if (ch >= 'a' && ch <= 'z')
cout << "LOWERCASE";
else
cout << "NONE";
✅ No library needed
✅ Full control
✅ Educational - shows ASCII logic
Method 2: Using ASCII values explicitly
if (ch >= 65 && ch <= 90)
cout << "UPPERCASE";
else if (ch >= 97 && ch <= 122)
cout << "LOWERCASE";
else
cout << "NONE";
✅ Functionally identical
❌ Less readable (magic numbers)
❌ Not recommended
Method 3: Using <cctype>
library functions
#include <cctype>
if (isupper(ch))
cout << "UPPERCASE";
else if (islower(ch))
cout << "LOWERCASE";
else
cout << "NONE";
✅ Clean and professional
✅ Self-documenting code
✅ Handles locale-specific characters
❌ Requires library include
Useful <cctype>
functions:
Function | Purpose | Example |
---|---|---|
isupper(ch) |
Check if uppercase | isupper('A') → true |
islower(ch) |
Check if lowercase | islower('a') → true |
isalpha(ch) |
Check if letter (any case) | isalpha('Z') → true |
isdigit(ch) |
Check if digit (0-9) | isdigit('5') → true |
isalnum(ch) |
Check if letter or digit | isalnum('A') → true |
isspace(ch) |
Check if whitespace | isspace(' ') → true |
toupper(ch) |
Convert to uppercase | toupper('a') → 'A' |
tolower(ch) |
Convert to lowercase | tolower('A') → 'a' |
When to use which approach?
Mistake 1: Using OR instead of AND
// ❌ WRONG!
if (ch >= 'A' || ch <= 'Z') // Using || instead of &&
cout << "UPPERCASE";
Why this is wrong:
'z'
(122)
122 >= 65
→ TRUETRUE || anything
→ TRUEMistake 2: Forgetting single quotes
// ❌ WRONG!
if (ch >= A && ch <= Z) // A and Z are undefined variables!
// ✅ CORRECT!
if (ch >= 'A' && ch <= 'Z') // 'A' and 'Z' are character literals
Mistake 3: Comparing with strings
// ❌ WRONG!
if (ch >= "A" && ch <= "Z") // "A" is a string, not a character!
Strings use double quotes ""
, characters use single quotes ''
Mistake 4: Wrong order of conditions
// ❌ WRONG!
if (ch >= 'Z' && ch <= 'A') // Impossible! No char is both ≥90 AND ≤65
Edge cases to test:
Input | ASCII | Expected Output | Why |
---|---|---|---|
'A' | 65 | UPPERCASE | First uppercase letter |
'Z' | 90 | UPPERCASE | Last uppercase letter |
'a' | 97 | LOWERCASE | First lowercase letter |
'z' | 122 | LOWERCASE | Last lowercase letter |
'@' | 64 | NONE | Just before 'A' |
'[' | 91 | NONE | Just after 'Z' |
'`' | 96 | NONE | Just before 'a' |
'{' | 123 | NONE | Just after 'z' |
'0' | 48 | NONE | Digit, not letter |
' ' | 32 | NONE | Space character |
Where character case checking is used:
1. Password Validation:
// Check password strength
bool hasUpper = false, hasLower = false;
for (char c : password) {
if (c >= 'A' && c <= 'Z') hasUpper = true;
if (c >= 'a' && c <= 'z') hasLower = true;
}
if (hasUpper && hasLower)
cout << "Strong password!";
2. Case-Insensitive Comparison:
// Compare strings ignoring case
// Convert both to lowercase first
char ch1 = 'A', ch2 = 'a';
if (tolower(ch1) == tolower(ch2))
cout << "Same letter!";
3. Text Processing & Formatting:
4. Data Validation:
// Validate name (only letters allowed)
bool isValidName = true;
for (char c : name) {
if (!((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == ' ')) {
isValidName = false;
break;
}
}
5. Encryption/Decryption (Caesar Cipher):
// Shift uppercase letters by 3
if (ch >= 'A' && ch <= 'Z') {
ch = ((ch - 'A' + 3) % 26) + 'A';
}
// 'A' → 'D', 'B' → 'E', etc.
6. Username Validation:
7. Syntax Highlighting (Code Editors):
8. Spell Checkers:
💡 Industry Tip: Character classification is fundamental to text processing, compilers, parsers, and natural language processing!
💡 Key Takeaway: Character comparisons use ASCII values behind the scenes. Range checking with >=
and <=
is perfect for continuous ranges like A-Z. Always use &&
for range checks, never ||
!
#include <iostream>
using namespace std;
int main() {
char ch;
cin >> ch;
if (ch >= 'A' && ch <= 'Z')
cout << "UPPERCASE";
else if (ch >= 'a' && ch <= 'z')
cout << "LOWERCASE";
else
cout << "NONE";
return 0;
}