Vowel/Consonant Check
Program to check if a character is a vowel or consonant
C++ Vowel/Consonant Check Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
This is useful in text processing, string manipulation, and language-related applications.
---
2. Header Files Used
#include <iostream>
cout for output and cin for input#include <cctype>
tolower() function used in this program---
3. Declaring Variables
char ch;
char is a data type that stores a single characterch will hold the character entered by the userExample:
ch = 'A' stores the character Ach = 'z' stores the character z---
4. Taking Input From User
cin >> ch;
chExample:
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:
A, E, I, O, U
a, e, i, o, u
Solution:
Convert everything to lowercase first, then check only lowercase vowels.
How tolower() works:
ch is uppercase (A-Z), it converts to lowercase (a-z)ch is already lowercase or not a letter, it returns unchangedExamples:
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:
ch equal to 'a'?OR
ch equal to 'e'?OR
ch equal to 'i'?OR
ch equal to 'o'?OR
ch equal to 'u'?If
any
of these is true, the character is a vowel.
Example:
ch = 'e': ch == 'e' istrue
→ entire condition is
true
→ prints "Vowel"
ch = 'b': All comparisons arefalse
→ 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 alphabetch <= 'z' → checks if character is 'z' or comes before 'z' in alphabet&& (AND) → both conditions must be trueWhat this checks:
Example:
ch = 'b': 'b' >= 'a' is true AND 'b' <= 'z' is true → prints "Consonant"ch = 'z': Both conditions true → prints "Consonant"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:
Then it must be:
Example:
'5'
: Not a vowel, not between 'a'-'z' → prints "Not an alphabet"
'@'
: Same result → prints "Not an alphabet"
---
9. Complete Example Walkthrough
Example 1: Vowel (uppercase)
E
ch = 'E'ch = tolower('E') → ch = 'e''e' == 'e' →true
→ prints "Vowel"
Example 2: Consonant
b
ch = 'b'ch = tolower('b') → ch = 'b' (unchanged)'b' >= 'a' && 'b' <= 'z' →true
→ prints "Consonant"
Example 3: Number
5
ch = '5'ch = tolower('5') → ch = '5' (unchanged)'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:
-
Total: 6 operations vs 10 operations
→ More efficient!
---
Summary
tolower() converts uppercase to lowercase for easier comparison||) checks if character matches any vowel&&) with range check identifies consonantsThis 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.