Vowel/Consonant Check in C++

Check vowel vs consonant in C++. Fold case or 'A' looks like a consonant.

BeginnerConditional ProgramsExample 3 of 4
vowel-consonant-check.cpp
1#include <iostream>
2#include <cctype>
3using namespace std;
4
5int main() {
6 char ch;
7
8 cout << "Enter a character: ";
9 cin >> ch;
10
11 // Convert to lowercase for easier checking
12 ch = tolower(ch);
13
14 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 }
21
22 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.