0
0
CppProgramBeginner · 2 min read

C++ Program to Count Vowels and Consonants

You can count vowels and consonants in C++ by reading a string and checking each character with if conditions for vowels, then incrementing vowel or consonant counters accordingly.
📋

Examples

Inputhello
OutputVowels: 2 Consonants: 3
InputProgramming
OutputVowels: 3 Consonants: 8
Input12345!@
OutputVowels: 0 Consonants: 0
🧠

How to Think About It

To count vowels and consonants, read the input string character by character. For each character, check if it is a letter. If it is, check if it is a vowel by comparing it to 'a', 'e', 'i', 'o', 'u' (case-insensitive). If yes, increase the vowel count; otherwise, increase the consonant count.
📐

Algorithm

1
Get input string from the user.
2
Initialize vowel and consonant counters to zero.
3
For each character in the string:
4
Check if the character is an alphabet letter.
5
If it is a vowel (a, e, i, o, u), increment vowel count.
6
Else if it is a consonant, increment consonant count.
7
Print the counts of vowels and consonants.
💻

Code

cpp
#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;
}
Output
Enter a string: Programming Vowels: 3 Consonants: 8
🔍

Dry Run

Let's trace the input 'hello' through the code

1

Input string

text = "hello"

2

Initialize counters

vowels = 0, consonants = 0

3

Check each character

h: isalpha -> true, tolower = 'h', not vowel, consonants = 1

4

Next character

e: isalpha -> true, tolower = 'e', vowel, vowels = 1

5

Next character

l: isalpha -> true, tolower = 'l', not vowel, consonants = 2

6

Next character

l: isalpha -> true, tolower = 'l', not vowel, consonants = 3

7

Next character

o: isalpha -> true, tolower = 'o', vowel, vowels = 2

CharacterIs Alpha?LowercaseVowel?Vowels CountConsonants Count
hYeshNo01
eYeseYes11
lYeslNo12
lYeslNo13
oYesoYes23
💡

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

Using switch-case for vowels
cpp
#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;
}
Switch-case can be clearer for vowel checking but is slightly longer.
Using a string of vowels and find()
cpp
#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;
}
Using <code>string::find()</code> makes vowel checking concise but may be slightly slower.

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.

ApproachTimeSpaceBest For
If conditionsO(n)O(1)Simple and fast for beginners
Switch-caseO(n)O(1)Clear structure for vowel checks
string::find()O(n)O(1)Concise code, easy to extend vowels
💡
Always convert characters to lowercase before checking vowels to simplify conditions.
⚠️
Counting non-alphabet characters as vowels or consonants instead of ignoring them.