Vowel/Consonant Check

Program to check if a character is a vowel or consonant

C++Beginner
C++
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char ch;
    
    cout << "Enter a character: ";
    cin >> ch;
    
    // Convert to lowercase for easier checking
    ch = tolower(ch);
    
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        cout << "Vowel" << endl;
    } else if (ch >= 'a' && ch <= 'z') {
        cout << "Consonant" << endl;
    } else {
        cout << "Not an alphabet" << endl;
    }
    
    return 0;
}

Output

Enter a character: E
Vowel

Vowel/Consonant Check in C++

This program teaches how to identify whether a character is a vowel or consonant in C++. It demonstrates character handling, case conversion, logical OR operators, and conditional statements. This is a fundamental program that helps students understand character manipulation and decision-making logic.

What This Program Does

The program:

  • Takes a single character as input from the user
  • Determines if it's a vowel (a, e, i, o, u)
  • Determines if it's a consonant (any other alphabet letter)
  • Handles both uppercase and lowercase letters
  • Identifies non-alphabetic characters

Converting to Lowercase

ch = tolower(ch);

Why convert to lowercase?

The program needs to check if the character is a vowel. Vowels are: ## a, e, i, o, u

But users might enter:

  • Uppercase: ## A, E, I, O, U
  • Lowercase: ## a, e, i, o, u

Solution: Convert everything to lowercase first, then check only lowercase vowels.

How tolower() works:

  • If ch is uppercase (A-Z), it converts to lowercase (a-z)
  • If ch is already lowercase or not a letter, it returns unchanged

Checking for Vowels

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

Understanding the Logical OR Operator (||):

The || operator means "OR" - if ## any one condition is true, the entire expression is true.

This condition checks:

  • Is ch equal to 'a'? ## OR
  • Is ch equal to 'e'? ## OR
  • Is ch equal to 'i'? ## OR
  • Is ch equal to 'o'? ## OR
  • Is ch equal to 'u'?

If ## any of these is true, the character is a vowel.

Checking for Consonants

else if (ch >= 'a' && ch <= 'z')

Understanding this condition:

  • ch >= 'a' → checks if character is 'a' or comes after 'a' in alphabet
  • ch <= 'z' → checks if character is 'z' or comes before 'z' in alphabet
  • && (AND) → both conditions must be true

What this checks:

  • Is the character between 'a' and 'z' (inclusive)?
  • If yes, it's a lowercase alphabet letter
  • Since we already checked for vowels, this must be a consonant

Summary

  • tolower() converts uppercase to lowercase for easier comparison
  • Logical OR (||) checks if character matches any vowel
  • Logical AND (&&) with range check identifies consonants
  • The program handles vowels, consonants, and non-alphabetic characters
  • This pattern is useful in text processing, word games, and language applications

This program teaches essential character manipulation skills that are used in string processing, text analysis, and many real-world applications like spell checkers, word processors, and language learning tools.