const References
Completed
Write int countVowels(const string &s). Passing by const reference avoids copying the string. Read n words and print the vowel count of each.
Input
n, then n words.
Output
n lines.
Example
Input
3 programming rhythm AEIOU
Output
3 0 5
Editor
C++
Hints
const string & means: no copy, and the function promises not to change it.
Solution explanation
Pass large objects by const & when you only need to read them.
Show solution
#include <iostream>
#include <string>
using namespace std;
int countVowels(const string &s) {
int count = 0;
for (char c : s) {
if (string("aeiouAEIOU").find(c) != string::npos) count++;
}
return count;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string w;
cin >> w;
cout << countVowels(w) << "\n";
}
return 0;
}