Vowel/Consonant Check in C++
Check vowel vs consonant in C++. Fold case or 'A' looks like a consonant.
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}
Output
Enter a character: E Vowel
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.