C++ Program to Count Vowels in String with Explanation
C++
Easy
Strings
30 views
1 min read
172 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around vowel, count, how. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Count how many vowels (a,e,i,o,u) are in string.
Real Life: Analyzing text composition.
Input Format
A string S.
Output Format
Total number of vowels in the string.
Constraints
• String length ≥ 1
• Count vowels: a, e, i, o, u
• Case-insensitive (A and a both counted)
Concept Explanation
We scan the string and count how many characters are vowels.
This helps in text analysis, like checking text composition.
Step-by-Step Logic
1.Take input string S.
2.Initialize count = 0.
3.Traverse the string character by character.
4.Convert character to lowercase (optional for easy checking).
5.If character is a, e, i, o, or u:
• Increment count.
6.Continue till end of string.
7.Print count.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void string_q3_count_vowels() {
string text = "Education is important";
int vowelCount = 0;
for(int i = 0; text[i] != '�'; i++) {
char ch = text[i];
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowelCount++;
}
}
cout << "String: " << text << endl;
cout << "Total vowels: " << vowelCount << endl;
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Count how many vowels (a,e,i,o,u) are in string.
Real Life: Analyzing text composition.
Input / Output
Output
Total number of vowels in the string.
Constraints
• String length ≥ 1
• Count vowels: a, e, i, o, u
• Case-insensitive (A and a both counted)
Explanation
Concept Explanation
We scan the string and count how many characters are vowels.
This helps in text analysis, like checking text composition.
Step-by-Step Explanation
1.Take input string S.
2.Initialize count = 0.
3.Traverse the string character by character.
4.Convert character to lowercase (optional for easy checking).
5.If character is a, e, i, o, or u:
• Increment count.
6.Continue till end of string.
7.Print count.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void string_q3_count_vowels() {
string text = "Education is important";
int vowelCount = 0;
for(int i = 0; text[i] != '�'; i++) {
char ch = text[i];
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowelCount++;
}
}
cout << "String: " << text << endl;
cout << "Total vowels: " << vowelCount << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!