首页 > 试题广场 >

编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该

[问答题]
编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音开始,有多少个单词以辅音开始,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符开始的单词,然后对于通过了isalpha()测试的单词,使用if或者switch语句来确定哪些以元音开始。该程序的运行情况如下:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 memters of lawn.   q
5 words beginning with vowels
4 words beginning with consonants
2 others
 #include<iostream>
 #include<cctype>
 #include<string>

 int main()
 {
    using namespace std;
   string word;
   int vowel=0;
   int consonant=0;
   int kind=0;
   cin>>word;
   while(word!="q")
   {
       if(isalpha(word[0]))
       {
           switch (word[0])
        {
            case 'a':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'e':
            case 'i':
            case 'o':
            case 'u':vowel++;break;
            default:consonant++;break;
       }

        }
       else
        kind++;
    cin>>word;
 }
 cout<<"vowel:"<<vowel<<endl;
 cout<<"consonant:"<<consonant<<endl;
 cout<<"others:"<<kind<<endl;
  return 0;
 }
发表于 2019-08-17 09:52:51 回复(0)