C++ 阅读单词的数量以元音、辅音开头
因此,在这个练习中,它应该阅读以元音、辅音开头的单词的数量,以及不符合这两个类别的单词的数量。到目前为止,我的代码是:C++ 阅读单词的数量以元音、辅音开头,c++,C++,因此,在这个练习中,它应该阅读以元音、辅音开头的单词的数量,以及不符合这两个类别的单词的数量。到目前为止,我的代码是: #include <iostream> int main() { using namespace std; char a; cout << "Enter words (q to quit)\n"; cin.get(a); int others = 0; int vowels =0; int consonan
#include <iostream>
int main()
{
using namespace std;
char a;
cout << "Enter words (q to quit)\n";
cin.get(a);
int others = 0;
int vowels =0;
int consonant =0;
while (a != 'q')
{
cin.get(a);
if(isalpha(a)) {
switch (a) {
case 'a':
case 'i':
case 'u':
case 'e':
case 'o':
vowels++;
break;
default:
consonant++;
break;
}
}
else {
others++;
}
}
cout << vowels << " words beginning with vowels\n";
cout << consonant << " words beginning with consonant\n";
cout << others << " others";
return 0;
}
#包括
int main()
{
使用名称空间std;
字符a;
coutcin.get(a)
读取单个字符(字母)。要读取单词,可以使用操作符>
和std::string
:
// make a std::string variable to hold a single word
string word;
// later read the word from the standard input
cin >> word;
cin.get(a)
读取单个字符(字母)。要读取单词,可以将操作符>
与std::string一起使用:
// make a std::string variable to hold a single word
string word;
// later read the word from the standard input
cin >> word;
你正在一个接一个地阅读字符,直到你点击“q”并分析所有字符。
如果是我,我可能只是将所有内容连接到一个字符串中,直到“q”为止,然后计算字符串。这可以通过在空间上拆分,然后循环生成的数组中的所有单词,并在每个单词的第一个字符的子字符串上进行大小写切换来完成。在点击“q”之前,您正在逐1读取字符并对它们进行分析。
如果是我,我可能会将所有内容连接成一个字符串,直到“q”然后对字符串进行评估,这可以通过在空间上的一个分割来完成,然后在结果数组中循环所有单词,并在每个单词的第一个字符的子串上做切换实例。 你的任务是一次读一个单词,只考虑那个单词的第一个字母,但是你的程序正在读取单个字符。(在每次迭代中使用cin.getc
)
此外,第一个字符在while循环外读取,并在检查它是否为“q”后立即丢弃
与您的作业更接近的可能是以下片段:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
using std::cout;
int others = 0,
vowels = 0,
consonants = 0;
std::string word;
cout << "Enter words (q to quit)\n";
// read a word at a time till a single 'q'
while ( std::cin >> word && word != "q" )
{
// consider only the first letter of the word
char c = word[0];
if ( std::isalpha(c) )
{
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
{
++vowels;
}
else
{
++consonants;
}
}
else
{
++others;
}
}
cout << vowels << " words beginning with vowels\n";
cout << consonants << " words beginning with consonant\n";
cout << others << " others";
return 0;
}
给出:
你的任务是一次读一个单词,只考虑那个单词的第一个字母,但是你的程序在每次迭代中读单个字符(使用<代码> CIN,GETC)。
此外,第一个字符在while循环外读取,并在检查它是否为“q”后立即丢弃
与您的作业更接近的可能是以下片段:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
using std::cout;
int others = 0,
vowels = 0,
consonants = 0;
std::string word;
cout << "Enter words (q to quit)\n";
// read a word at a time till a single 'q'
while ( std::cin >> word && word != "q" )
{
// consider only the first letter of the word
char c = word[0];
if ( std::isalpha(c) )
{
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
{
++vowels;
}
else
{
++consonants;
}
}
else
{
++others;
}
}
cout << vowels << " words beginning with vowels\n";
cout << consonants << " words beginning with consonant\n";
cout << others << " others";
return 0;
}
给出:
你需要标记字符串。看一看。使用较小的测试用例通常可以更容易地确定出错的地方。例如,你可以从“the”和“Ox”开始。一个很好的字符串拆分方法也是:它真的很奇怪……如果我输入“the”输出是1个单词的元音,2个单词的辅音,0个其他。即使它不是真的,它也能正确地读取元音和辅音。但是当它“很棒”时输出为3个单词的元音,4个单词的辅音,0个其他。这不是真的,而且它没有正确地读取元音和辅音。dis代码有误。.XD您在while之外读取第一个字符,但您使用它只是为了查看它是否为“q”,然后在进入循环后立即读取另一个字符。尝试读取一个字符,最好是单个单词(否则你如何区分“quit”和“q”?),只有在while循环的情况下。你需要标记字符串。看一看。使用较小的测试用例通常可以更容易地确定哪里出了问题。例如,你可以从“the”和“Ox”开始。一个很好的字符串拆分方法也是:这真的很奇怪……如果我输入“the”输出是1个单词的元音,2个单词的辅音,0个其他。即使它不是真的,它也能正确地读取元音和辅音。但是当它“很棒”时输出为3个单词的元音,4个单词的辅音,0个其他。这不是真的,而且它没有正确地读取元音和辅音。dis代码有误。.XD您在while之外读取第一个字符,但您使用它只是为了查看它是否为“q”,然后在进入循环后立即读取另一个字符。尝试读取一个字符,最好是单个单词(否则,您如何区分“退出”和“q”?),仅在while循环的条件下。@asdfg1234检查您发布的上一个代码的结果。它不在编译器中吗?我的输出:5个以元音开头的单词,4个以辅音开头的单词,2个others@asdfg1234考虑FI时的情况第一个单词是以元音开头的,那是因为我太懒了,不想把元音的首字母大写xd@asdfg1234检查您发布的上一个代码的结果。它不是在您的编译器中吗?我的输出:5个以元音开头的单词,4个以con开头的单词索南特,2others@asdfg1234考虑第一个单词用元音开头的情况,那是因为我太懒了,不能把元音放在元音LOL XD上。
5 words beginning with vowels
4 words beginning with consonant
2 others