7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,
该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。
为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,
然后对于通过了isalpha()测试的单词,
使用if或 switch 语句来确定哪些以元音打头。
该程序的运行情况如下:
Enter words(g to quit) :
**The 12 awesome oxen ambled
quietly across 15 meters of lawn.q * *
5 words beginning with vowels
4 words beginning with consonants
2 others
说明:这里有一个难点,就是大家一定要知道元音字母,辅音字母,和其他类型的符号的输入
#pragma region 第五章练习7
/*
*/
//元音字母:a,e,i,o,u.包括大写的
#if 1
#include<iostream>
#include <array>
int main(){
using namespace std;
cout << "请输入一个单词(字母q退出):";
string temp;
unsigned cntworld1 = 0;//其他符号
unsigned cntworld2 = 0;//元音字母
unsigned cntworld3 = 0;//辅音
char firstCh;
while (cin >> temp && "q" != temp)
{
firstCh = tolower(temp[0]);
if (!isalpha(firstCh))
{
++cntworld1;
}
else if(
firstCh == 'a' ||
firstCh == 'e' ||
firstCh == 'i' ||
firstCh == 'r' ||
firstCh == 'u'
)
{
++cntworld2;
}
else
{
++cntworld3;
}
}
cout << "元音开头的单词:" << cntworld2 << " 个,辅音单词" << cntworld3
<< " 个,其他 " << cntworld1 << " 个" << endl;
return 0;
}
#endif
#pragma endregion
//对输入的符号线上面转换为了小写字母,在判断的时候只考虑小写字母的情况就是了,大写字母不考虑,应为都转换过来了。上面没做判断,直接转换