输入一个句子(以句号结束),统计改句子中的元音字母数、辅音字母数、空格数、数字数及其他字符数
while循环(字符)
#include <iostream>
using namespace std;
int main()
{
char ch;
int numvowel = 0, numcons = 0, numspace = 0, numdigit = 0, numother = 0;
cout << "请输入句子:";
cin.get(ch);
while (ch != '.')
{//处理每个字符
if (ch >= 'A' && ch <= 'Z') ch = ch - 'A' + 'a';//将大写字母转换为小写字母
if (ch >= 'a' && ch <= 'z')
{
if (ch == 'a'|| ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++numvowel;
else ++numcons;
}
else if (ch == ' ') ++numspace;
else if (ch > '0' && ch < '9') ++numdigit;
else ++numother;
cin.get(ch);
}
cout << "元音字母数:" << numvowel << endl;
cout << "辅音字母数:" << numcons << endl;
cout << "空格数:" << numspace << endl;
cout << "数字字符数:" << numdigit << endl;
cout << "其他字符数:" << numother << endl;
return 0;
}
运行结果: