1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> using namespace std; int main() { char ch; // Input the character cout << "Enter a character: "; cin >> ch; // Check if the character is uppercase or lowercase using a switch statement switch (ch) { case 'A' ... 'Z': // Range for uppercase letters cout << "The character '" << ch << "' is an uppercase letter." << endl; break; case 'a' ... 'z': // Range for lowercase letters cout << "The character '" << ch << "' is a lowercase letter." << endl; break; default: cout << "The character '" << ch << "' is not an alphabetic letter." << endl; break; } return 0; } |
Explanation:
- Input:
- The program prompts the user to enter a single character.
- Switch Statement:
- The
switch
statement checks the value ofch
:case 'A' ... 'Z'
: Matches any uppercase letter (‘A’ to ‘Z’). If the character is uppercase, it prints a corresponding message.case 'a' ... 'z'
: Matches any lowercase letter (‘a’ to ‘z’). If the character is lowercase, it prints a corresponding message.default
: Handles any input that is not an alphabetic letter, such as digits or special characters.
- The
- Output:
- The program prints whether the entered character is an uppercase or lowercase letter, or if it’s not an alphabetic letter.
Example Outputs:
Example 1: Uppercase Letter
Input:
- Character:
G
Output:
1 2 |
Enter a character: G The character 'G' is an uppercase letter. |
Example 2: Lowercase Letter
Input:
- Character:
m
Output:
1 2 |
Enter a character: m The character 'm' is a lowercase letter. |
Example 3: Non-Alphabetic Character
Input:
- Character:
9
Output:
1 2 |
Enter a character: 9 The character '9' is not an alphabetic letter. |