C++ Program to Check Vowel or Consonant
In C++, you can check if a character is a vowel or consonant by using
if statements to compare the character against vowels like 'a', 'e', 'i', 'o', 'u' (both lowercase and uppercase), and print the result accordingly.Examples
Inputa
Outputa is a vowel
InputB
OutputB is a consonant
Input1
Output1 is not an alphabet
How to Think About It
To decide if a character is a vowel or consonant, first check if it is a letter. If it is not a letter, say it is not an alphabet. If it is a letter, compare it to vowels (a, e, i, o, u) in both lowercase and uppercase. If it matches any vowel, it is a vowel; otherwise, it is a consonant.
Algorithm
1
Get a single character input from the user.2
Check if the character is an alphabet letter.3
If not an alphabet, print it is not an alphabet and stop.4
If it is an alphabet, check if it matches any vowel (a, e, i, o, u) ignoring case.5
If it matches a vowel, print it is a vowel.6
Otherwise, print it is a consonant.Code
cpp
#include <iostream> using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) { cout << ch << " is not an alphabet" << endl; } else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { cout << ch << " is a vowel" << endl; } else { cout << ch << " is a consonant" << endl; } return 0; }
Output
Enter a character: a
a is a vowel
Dry Run
Let's trace the input 'a' through the code
1
Input character
User inputs 'a'
2
Check if alphabet
'a' is between 'a' and 'z', so it is an alphabet
3
Check if vowel
'a' matches vowel 'a'
4
Print result
Print 'a is a vowel'
| Step | Condition Checked | Result |
|---|---|---|
| 2 | 'a' is alphabet? | True |
| 3 | 'a' is vowel? | True |
| 4 | Print output | 'a is a vowel' |
Why This Works
Step 1: Check if character is alphabet
We use if to see if the character is between 'A' and 'Z' or 'a' and 'z' to confirm it is a letter.
Step 2: Check vowels
We compare the character with vowels in both lowercase and uppercase using if and || operators.
Step 3: Print result
If it matches a vowel, print it is a vowel; otherwise, print it is a consonant.
Alternative Approaches
Using switch-case
cpp
#include <iostream> #include <cctype> using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) { cout << ch << " is not an alphabet" << endl; } else { switch(tolower(ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is a consonant" << endl; } } return 0; }
This uses <code>switch</code> and <code>tolower</code> to simplify vowel checking and improve readability.
Using string find
cpp
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { char ch; string vowels = "aeiouAEIOU"; cout << "Enter a character: "; cin >> ch; if (!isalpha(ch)) { cout << ch << " is not an alphabet" << endl; } else if (vowels.find(ch) != string::npos) { cout << ch << " is a vowel" << endl; } else { cout << ch << " is a consonant" << endl; } return 0; }
This uses a string of vowels and <code>find</code> method to check membership, making the code concise.
Complexity: O(1) time, O(1) space
Time Complexity
The program runs in constant time because it only checks a single character with fixed comparisons.
Space Complexity
The program uses constant space for variables and no extra data structures.
Which Approach is Fastest?
All approaches run in O(1) time; using switch or string find improves readability but does not affect speed significantly.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else comparisons | O(1) | O(1) | Simple and direct checks |
| Switch-case with tolower | O(1) | O(1) | Cleaner code with multiple cases |
| String find method | O(1) | O(1) | Concise code using standard library |
Always check if the input is an alphabet before checking vowels or consonants to avoid errors.
Beginners often forget to check uppercase vowels or non-alphabet characters, causing wrong results.