题目:
编写一个程序,她每次读取一个单词,知道用户只输入q。然后,该程序指出有多少个单词以元音大头,而多少个单词以辅音大头,还有多少个单词不属于着两类。
源代码:
#include <iostream>
#include <cctype>
//元音:A、E、I、O、U
int main()
{
using namespace std;
char word[20];
int vowels_num = 0, consonants_num = 0,other_num = 0;
cout << "请输入句子: " << endl;
while (cin >> word && (word[0] != 'q' || word[1] != '\0'))
//(word[0] != 'q' || word[1] != '\0')防止与以q开头的字母混淆
{
word[0] = tolower(word[0]); //防止大小写混淆,统一变小写
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
{
++vowels_num;
}
else if (!isdigit(word[0])) //不为元音开头,还不是数字就是以辅音开头
{
++consonants_num;
}
else
++other_num;
}
cout << "元音开头单词: " << vowels_num << endl
<< "辅音开头单词: " << consonants_num << endl
<< "其他单词: " << other_num;
return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈
标签:word,cout,++,练习,C++,单词,num,40,元音 From: https://blog.csdn.net/little_startoo/article/details/142298456