显示具有最小给定元音数量的文件中的单词 [英] Show words from a file with a minimum given number of vowels
问题描述
我正在尝试解决问题,但不知道如何解决。
问题是,给定一个输入文件,该文件的第一行具有数字k,第二行具有一些单词,K是必须在控制台中打印一个单词的最小元音数量。 / p>
I'm trying to solve a problem and i can't figure out how to do it. The problem says that given an input file which has on the first line a number k and on the next lines some words, K is the minimum number of vowels that a word needs to have to be printed in the console.
Example:
Input: test.in
cars
are...great and awesome
Output:
If k is 2 the result should be:
are
great
awesome
And also it should not take into account spaces or other characters like .,!,? and so on.
到目前为止我所拥有的:
What i have so far:
int main(){
ifstream fin("dataTest.in");
char s[250];
int k;
fin>>k;
int nrVowels=0;
while(fin>>s) {
int n = strlen(s);
for(int i=0; i < n; i++)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'o' || s[i] == 'i' || s[i] == 'u'){
nrVowels++;
if(nrVowels == k){
cout<<"Here i guess i should print the word?";
}
}
}
}
}
正如您从代码中看到的那样,我的问题是我不确定我应该如何打印单词以及如何知道单词的结尾,因为我不想只打印这个词的一部分。
你们对我应该怎么做有任何想法吗?
As you can see from the code my problem is that i'm not really sure how i should print the word and how would i know where the word is ending because i don't want to print only a part of the word. Do you guys have any ideas on how i should do this?
推荐答案
std::ifstream file("Read.txt");
int number;
file>>number;
std::string str;
auto vowels = [](char c){return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u';};
while (std::getline(file, str))
{
if(std::count_if(str.cbegin(), str.cend(), vowels) >= number){
std::cout<<str<<'\n';
}
}
这篇关于显示具有最小给定元音数量的文件中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!