Vowel/Consonant Check

Program to check if a character is a vowel or consonant

BeginnerTopic: Conditional Programs
Back

C++ Vowel/Consonant Check Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

Understanding Vowel/Consonant Check

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.

---

1. 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

This is useful in text processing, string manipulation, and language-related applications.

---

2. Header Files Used

#include <iostream>

Provides cout for output and cin for input

#include <cctype>

Provides character manipulation functions
Contains tolower() function used in this program
This header is essential for character case conversion

---

3. Declaring Variables

char ch;

char is a data type that stores a single character
ch will hold the character entered by the user
Characters are stored as ASCII values internally

Example:

ch = 'A' stores the character A
ch = 'z' stores the character z

---

4. Taking Input From User

`cout << "Enter a character: ";`

cin >> ch;

First line prompts the user to enter a character
Second line reads a single character and stores it in ch

Example:

If user enters

E

, then ch = 'E'

---

5. 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

Examples:

tolower('E') → returns 'e'
tolower('a') → returns 'a'
tolower('5') → returns '5' (unchanged)

After this line, ch will always be lowercase if it was an uppercase letter.

---

6. 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.

Example:

If ch = 'e': ch == 'e' is

true

→ entire condition is

true

→ prints "Vowel"

If ch = 'b': All comparisons are

false

→ moves to next condition

---

7. 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

Example:

If ch = 'b': 'b' >= 'a' is true AND 'b' <= 'z' is true → prints "Consonant"
If ch = 'z': Both conditions true → prints "Consonant"
If ch = 'A': After tolower(), it becomes 'a', but if somehow it's still 'A', then 'A' >= 'a' is false → moves to else

---

8. Handling Non-Alphabetic Characters

else

cout << "Not an alphabet" << endl;

If the character is:

Not a vowel (first condition false)
Not between 'a' and 'z' (second condition false)

Then it must be:

A number (0-9)
A special character (!, @, #, etc.)
A space or other non-alphabetic character

Example:

If user enters

'5'

: Not a vowel, not between 'a'-'z' → prints "Not an alphabet"

If user enters

'@'

: Same result → prints "Not an alphabet"

---

9. Complete Example Walkthrough

Example 1: Vowel (uppercase)

User enters:

E

ch = 'E'
ch = tolower('E')ch = 'e'
Check: 'e' == 'e'

true

→ prints "Vowel"

Example 2: Consonant

User enters:

b

ch = 'b'
ch = tolower('b')ch = 'b' (unchanged)
Check vowels: All false
Check: 'b' >= 'a' && 'b' <= 'z'

true

→ prints "Consonant"

Example 3: Number

User enters:

5

ch = '5'
ch = tolower('5')ch = '5' (unchanged)
Check vowels: All false
Check: '5' >= 'a' && '5' <= 'z'

false

→ prints "Not an alphabet"

---

10. Why This Approach is Better

Alternative approach (without tolower):

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

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

This would require checking 10 conditions (5 lowercase + 5 uppercase).

Our approach:

Convert to lowercase first (1 operation)
Check only 5 lowercase vowels

-

Total: 6 operations vs 10 operations

→ More efficient!

---

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.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Vowel/Consonant Check

This C++ program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents