Check whether the given character is a Vowel or Consonant
English alphabet has 26 letters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Vowels (5 letters):
Total: 10 possibilities (5 uppercase + 5 lowercase)
Consonants (21 letters):
B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z
Total: 42 possibilities (21 uppercase + 21 lowercase)
Why this classification matters:
The challenge: We need to check if a character matches ANY of 10 possible vowels!
Solution: Chain OR operators (||)
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
How it works:
ch == 'a'
โ If TRUE, entire condition is TRUE โ
ch == 'e'
โ If TRUE, entire condition is TRUE โ
ch == 'i'
โ And so on...Evaluation order (short-circuit):
Input | First Match | Checks Done |
---|---|---|
'a' | ch == 'a' | 1 (stops immediately) |
'E' | ch == 'E' | 6 (after checking a,e,i,o,u,A) |
't' | None | All 10 (all fail) |
๐ก Efficiency tip: Put most common cases first! In English, 'e' is the most common vowel.
The problem: Users can input either case!
Examples of valid inputs:
'a'
โ Lowercase vowel'A'
โ Uppercase vowel'e'
โ Lowercase vowel'E'
โ Uppercase vowelWhy they're different in C++:
Character | ASCII Value | Binary |
---|---|---|
'a' | 97 | 01100001 |
'A' | 65 | 01000001 |
'e' | 101 | 01100101 |
'E' | 69 | 01000101 |
๐ Notice: Lowercase and uppercase have different ASCII values!
Difference is always 32 (lowercase = uppercase + 32)
Two approaches to handle this:
Approach 1: Check both cases (our solution)
if (ch == 'a' || ch == 'A' || ... )
โ
Simple and direct
โ
No extra functions needed
โ More code to write
Approach 2: Convert to one case first
#include <cctype>
ch = tolower(ch); // Convert to lowercase
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
โ
Less conditions to check
โ
Cleaner code
โ Requires extra header and function
๐ก For this problem, our approach is better for learning fundamentals!
Example 1: Input 't' (consonant)
ch = 't'
't' == 'a'
โ FALSE โ't' == 'e'
โ FALSE โ't' == 'i'
โ FALSE โ't' == 'o'
โ FALSE โ't' == 'u'
โ FALSE โ't' == 'A'
โ FALSE โ't' == 'E'
โ FALSE โ't' == 'I'
โ FALSE โ't' == 'O'
โ FALSE โ't' == 'U'
โ FALSE โelse
Example 2: Input 'o' (lowercase vowel)
ch = 'o'
'o' == 'a'
โ FALSE โ'o' == 'e'
โ FALSE โ'o' == 'i'
โ FALSE โ'o' == 'o'
โ TRUE โ
MATCH FOUND!Example 3: Input 'A' (uppercase vowel)
ch = 'A'
'A' == 'A'
โ TRUE โ
MATCH FOUND!Example 4: Input 'Z' (uppercase consonant)
ch = 'Z'
else
Method 1: Chained OR (Our Solution)
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
cout << "Vowel";
} else {
cout << "Consonant";
}
โ
Clear and explicit
โ
No extra libraries needed
โ
Beginner-friendly
Method 2: Using tolower() function
#include <cctype>
char lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
cout << "Vowel";
} else {
cout << "Consonant";
}
โ
Fewer comparisons
โ
Easier to read
โ Requires cctype library
Method 3: Using switch statement
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
cout << "Vowel";
break;
default:
cout << "Consonant";
}
โ
Very readable
โ
Efficient for many cases
โ Haven't learned switch yet
Method 4: Using string find() (Advanced)
#include <string>
string vowels = "aeiouAEIOU";
if (vowels.find(ch) != string::npos) {
cout << "Vowel";
} else {
cout << "Consonant";
}
โ
Very concise
โ
Easy to modify vowel list
โ Advanced concept (strings)
๐ก For this stage of learning, Method 1 (our solution) is best!
Testing all vowels (lowercase):
Input | Expected | Result |
---|---|---|
'a' | Vowel | โ |
'e' | Vowel | โ |
'i' | Vowel | โ |
'o' | Vowel | โ |
'u' | Vowel | โ |
Testing all vowels (uppercase):
Input | Expected | Result |
---|---|---|
'A' | Vowel | โ |
'E' | Vowel | โ |
'I' | Vowel | โ |
'O' | Vowel | โ |
'U' | Vowel | โ |
Testing consonants (various):
Input | Expected | Result |
---|---|---|
'b' | Consonant | โ |
't' | Consonant | โ |
'Z' | Consonant | โ |
'm' | Consonant | โ |
๐ก Edge case: What if user enters a number or special character? It will output "Consonant" (since it's not a vowel). In production code, we'd validate input first!
Where vowel/consonant checking is used:
1. Text-to-Speech Systems:
2. Word Games:
3. Encryption & Codes:
4. Natural Language Processing (NLP):
5. Grammar Checkers:
6. Educational Software:
๐ก Fun fact: The vowel frequency in English text is ~38%! 'E' alone accounts for ~12% of all letters.
๐ก Key Takeaway: Chaining multiple OR conditions lets you check if a value matches ANY option in a list. This is fundamental for classification problems!
#include <iostream>
using namespace std;
int main() {
char ch;
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
cout << "Vowel";
else
cout << "Consonant";
return 0;
}