Vowel/Consonant Check in C++
Check whether a character is a vowel, a consonant, or not a letter in C++. Uses tolower from <cctype>, the || operator, and a range test on 'a' to 'z'.
1#include <iostream>2#include <cctype>3using namespace std;45int main() {6 char ch;78 cout << "Enter a character: ";9 cin >> ch;1011 // Convert to lowercase for easier checking12 ch = tolower(ch);1314 if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {15 cout << "Vowel" << endl;16 } else if (ch >= 'a' && ch <= 'z') {17 cout << "Consonant" << endl;18 } else {19 cout << "Not an alphabet" << endl;20 }2122 return 0;23}
Sample Output
Vowel
E became e, which matched the vowel list.
In this one
- How tolower makes a check case-insensitive
- How || joins several equality tests
- How a range test finds letters
- Why digits and symbols need their own branch
How it works
- 1
Read one character (E in the sample).
- 2
tolower turns E into e.
- 3
The vowel test finds ch == 'e' and prints Vowel.
- 4
Other letters fall to the range test and print Consonant.
- 5
Anything else prints Not an alphabet.
Edge cases
Digit input
Enter 5.
tolower leaves it unchanged, it is not a vowel, and it is outside a–z, so the program prints Not an alphabet.
Letter y
Enter y.
It is in the range a–z and not in the vowel list, so it prints Consonant. The program follows the five-vowel rule.
What's going on
This C++ program classifies a character as vowel, consonant, or not a letter. Compare against a e i o u after folding case, or 'A' looks like a consonant.
#include <cctype>
That's where tolower lives.
char ch;
cin >> ch;
One character. cin skips spaces, so a typed space is not what you get.
ch = tolower(ch);
'E' becomes 'e'. Skip this and the sample input E fails every vowel check and prints "Consonant" or worse.
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
Any one match is enough. || stops at the first true. A string plus find is the same test with less typing.
else if (ch >= 'a' && ch <= 'z')
Letter, but not a vowel → consonant. y lands here. English sometimes treats y as a vowel. This program does not.
else
Digits, punctuation, leftover symbols: "Not an alphabet".
Sample: E → e → vowel. Matches the output.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does tolower('Q') return?
Pick an answer to check it.
Question 2
Input is #. Which line prints?
Pick an answer to check it.
Questions people ask
Why convert to lowercase first?
It halves the number of comparisons. Without it you need to check both 'a' and 'A', 'e' and 'E', and so on.
Could I use isalpha instead of the range test?
Yes. isalpha(ch) from <cctype> returns nonzero for letters and is clearer.
What if the user types a word?
cin >> ch takes only the first character. The rest stays in the buffer.