C++ Program to Count Vowels and Consonants
if conditions for vowels, then incrementing vowel or consonant counters accordingly.Examples
How to Think About It
Algorithm
Code
#include <iostream> #include <cctype> using namespace std; int main() { string text; int vowels = 0, consonants = 0; cout << "Enter a string: "; getline(cin, text); for (char ch : text) { if (isalpha(ch)) { char lower = tolower(ch); if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++; else consonants++; } } cout << "Vowels: " << vowels << "\n"; cout << "Consonants: " << consonants << "\n"; return 0; }
Dry Run
Let's trace the input 'hello' through the code
Input string
text = "hello"
Initialize counters
vowels = 0, consonants = 0
Check each character
h: isalpha -> true, tolower = 'h', not vowel, consonants = 1
Next character
e: isalpha -> true, tolower = 'e', vowel, vowels = 1
Next character
l: isalpha -> true, tolower = 'l', not vowel, consonants = 2
Next character
l: isalpha -> true, tolower = 'l', not vowel, consonants = 3
Next character
o: isalpha -> true, tolower = 'o', vowel, vowels = 2
| Character | Is Alpha? | Lowercase | Vowel? | Vowels Count | Consonants Count |
|---|---|---|---|---|---|
| h | Yes | h | No | 0 | 1 |
| e | Yes | e | Yes | 1 | 1 |
| l | Yes | l | No | 1 | 2 |
| l | Yes | l | No | 1 | 3 |
| o | Yes | o | Yes | 2 | 3 |
Why This Works
Step 1: Check if character is a letter
Using isalpha() ensures we only count letters, ignoring digits and symbols.
Step 2: Convert to lowercase
Using tolower() makes vowel checking case-insensitive, so 'A' and 'a' are treated the same.
Step 3: Count vowels and consonants
If the character matches any vowel, increment vowel count; otherwise, increment consonant count.
Alternative Approaches
#include <iostream> #include <cctype> using namespace std; int main() { string text; int vowels = 0, consonants = 0; cout << "Enter a string: "; getline(cin, text); for (char ch : text) { if (isalpha(ch)) { switch (tolower(ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': vowels++; break; default: consonants++; } } } cout << "Vowels: " << vowels << "\n"; cout << "Consonants: " << consonants << "\n"; return 0; }
#include <iostream> #include <cctype> using namespace std; int main() { string text, vowels_str = "aeiou"; int vowels = 0, consonants = 0; cout << "Enter a string: "; getline(cin, text); for (char ch : text) { if (isalpha(ch)) { char lower = tolower(ch); if (vowels_str.find(lower) != string::npos) vowels++; else consonants++; } } cout << "Vowels: " << vowels << "\n"; cout << "Consonants: " << consonants << "\n"; return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through each character once, so time grows linearly with input size.
Space Complexity
Only a few counters and variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
All approaches run in O(n) time; using if conditions or switch is slightly faster than string::find() due to less overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If conditions | O(n) | O(1) | Simple and fast for beginners |
| Switch-case | O(n) | O(1) | Clear structure for vowel checks |
| string::find() | O(n) | O(1) | Concise code, easy to extend vowels |