为此,方法之一是,使用isalpha()来区分字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning consotants
2 others
#include <iostream>
using namespace std;
int main() {
cout << "enter words(q to quit)\n";
string word;
cin >> word;
int type1 = 0, type2 = 0, type3 = 0;
while (word != "q") {
if (isalpha(word[0])) {
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
type1++;
else
type2++;
}
else
type3++;
cin >> word;
}
cout << type1 << " words beginning with vowels.\n";
cout << type2 << " words beginning with consonants.\n";
cout << type3 << "others.\n";
return 0;
}