10. Check Vowel or Consonant

Easy โฑ๏ธ 8 min

Problem Statement

Check whether the given character is a Vowel or Consonant

Input Format:
Enter a Character as input
Output Format:
Print the output as "Vowel" or "Consonant"
Constraints:
0 <= letter <= 2^7
Sample Input 1:
t
Sample Output 1:
Consonant
Sample Input 2:
o
Sample Output 2:
Vowel
๐Ÿ”ค Character Classification with Chained OR Operators
๐Ÿ“š What Are Vowels and Consonants? โ–ผ

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):

  • A (or a)
  • E (or e)
  • I (or i)
  • O (or o)
  • U (or u)

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:

  • ๐Ÿ—ฃ๏ธ Vowels are pronounced with open vocal tract
  • ๐Ÿ”ค Every English word contains at least one vowel
  • ๐Ÿ“– Critical for grammar, spelling, and linguistics
  • ๐Ÿ’ป Used in text processing, word games, autocorrect, etc.
โ›“๏ธ Chaining Multiple OR Conditions โ–ผ

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:

  1. Check ch == 'a' โ†’ If TRUE, entire condition is TRUE โœ…
  2. If FALSE, check ch == 'e' โ†’ If TRUE, entire condition is TRUE โœ…
  3. If FALSE, check ch == 'i' โ†’ And so on...
  4. If ANY comparison is TRUE โ†’ Execute the "Vowel" block
  5. If ALL comparisons are FALSE โ†’ It's a consonant

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.

๐ŸŽฏ Why Check Both Cases (Uppercase & Lowercase)? โ–ผ

The problem: Users can input either case!

Examples of valid inputs:

  • 'a' โ†’ Lowercase vowel
  • 'A' โ†’ Uppercase vowel
  • 'e' โ†’ Lowercase vowel
  • 'E' โ†’ Uppercase vowel

Why 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!

๐Ÿ” Step-by-Step Execution Examples โ–ผ

Example 1: Input 't' (consonant)

  1. Read: ch = 't'
  2. Check: 't' == 'a' โ†’ FALSE โŒ
  3. Check: 't' == 'e' โ†’ FALSE โŒ
  4. Check: 't' == 'i' โ†’ FALSE โŒ
  5. Check: 't' == 'o' โ†’ FALSE โŒ
  6. Check: 't' == 'u' โ†’ FALSE โŒ
  7. Check: 't' == 'A' โ†’ FALSE โŒ
  8. Check: 't' == 'E' โ†’ FALSE โŒ
  9. Check: 't' == 'I' โ†’ FALSE โŒ
  10. Check: 't' == 'O' โ†’ FALSE โŒ
  11. Check: 't' == 'U' โ†’ FALSE โŒ
  12. All FALSE โ†’ Go to else
  13. Print: "Consonant"

Example 2: Input 'o' (lowercase vowel)

  1. Read: ch = 'o'
  2. Check: 'o' == 'a' โ†’ FALSE โŒ
  3. Check: 'o' == 'e' โ†’ FALSE โŒ
  4. Check: 'o' == 'i' โ†’ FALSE โŒ
  5. Check: 'o' == 'o' โ†’ TRUE โœ… MATCH FOUND!
  6. Stop checking (short-circuit evaluation)
  7. Entire condition is TRUE
  8. Print: "Vowel"
  9. Exit (never checks A, E, I, O, U)

Example 3: Input 'A' (uppercase vowel)

  1. Read: ch = 'A'
  2. Check lowercase vowels: All FALSE (a,e,i,o,u)
  3. Check: 'A' == 'A' โ†’ TRUE โœ… MATCH FOUND!
  4. Stop checking
  5. Print: "Vowel"

Example 4: Input 'Z' (uppercase consonant)

  1. Read: ch = 'Z'
  2. Check all 10 vowels: All FALSE โŒ
  3. Go to else
  4. Print: "Consonant"
๐ŸŽจ Alternative Solutions & Approaches โ–ผ

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!

๐Ÿงช Complete Test Cases โ–ผ

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!

๐ŸŒ Real-World Applications โ–ผ

Where vowel/consonant checking is used:

1. Text-to-Speech Systems:

  • Pronunciation rules differ for vowels vs consonants
  • Voice synthesis needs to know letter types
  • Used in Siri, Alexa, Google Assistant

2. Word Games:

  • Scrabble/Wordle scoring systems
  • Wheel of Fortune (vowels cost money!)
  • Hangman strategy algorithms

3. Encryption & Codes:

  • Pig Latin translator (vowel handling)
  • Custom cipher systems
  • Password strength checkers

4. Natural Language Processing (NLP):

  • Text analysis and statistics
  • Readability scoring
  • Language detection
  • Spell checkers

5. Grammar Checkers:

  • Determining "a" vs "an" (an apple, a tree)
  • Syllable counting
  • Rhyme detection in poetry

6. Educational Software:

  • Teaching kids letter sounds
  • Reading comprehension tools
  • Language learning apps

๐Ÿ’ก 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!

Solution

#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;
}