我正在尝试创建一个程序来计算用户给出的单词中元音和辅音的数量。我使用 strlen() 之类的函数来获取用户输入数组的长度以进行迭代。我还使用了 bits/stdc++.h,所以我可以为用户输入中出现的任何元音调用 count() 函数。
为了检查元音的出现,我尝试了几个函数,从 count 到 find_first_of,再到 find(),再到 count()。我的第一个错误首先是说它无法识别对 strlen() 的调用。我检查以确保我包含了正确的包以使用 strlen(),但这似乎不是问题。我在装有 High Sierra 的 MacBook Pro 上运行这个程序。
#include <iostream>//std::cout
#include <string>//std::string
#include <cstdlib>
#include <set>// std::size_t
#include "/Users/Desktop/stdc++.h"
using namespace std;
int main(){
string input = "hello";
cout << " The input is " << input << endl;
std::size_t vowelsFound = 0;
/*cout << " Enter a word, and we will count the vowels and consonants: ";
cin >> input;
cout << " You entered " << input << endl;*/
char cons[] = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
char vows[] = {'a','e','i','o','u','y'};
std::size_t n = strlen(input);
std::size_t v = strlen(vows);
for(int i = 0; i < v; i++){
if(count(input,input + n,vows[i]) != 0){
vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows[] encountered in input
cout << " The vowel(s) found is " << vowelsFound << endl;
}
else{
cout << " The vowel " << vows[i] << " is not found in input " << endl;
}
}
cout << " The amount of vowels found is " << vowelsFound << endl;
cout << " The expected amount of vowels found is 2 " << endl;
}
我硬编码了短语“hello”作为输入,所以当一切都说完后,元音计数应该是 2。