使用两个循环完成,外一个字符串数组,数组中一个特定字符串的内部字符。完整的例子:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int countAllVowels(std::string array[], int n){
std::vector<char> vowels = { 'a', 'e', 'i', 'o', 'u' };
int countvowels = 0;
for (int i = 0; i < n; i++){
for (auto ch : array[i]){
if (std::find(vowels.begin(), vowels.end(), ch) != vowels.end()){
countvowels++;
}
}
}
return countvowels;
}
int main(){
std::string arr[] = { "hello", "world", "the quick", "brown fox" };
std::cout << "The number of vowels is: " << countAllVowels(arr, 4);
}
来源
2017-08-02 22:42:49
Ron
std :: string has operator []并知道它的大小。 – 2017-08-02 21:37:03
分解问题:编写一个函数来首先计算单个字符串中的元音。 – hyde