统计句子中各种字符的出现次数

输入一个句子(以句号结束),统计改句子中的元音字母数、辅音字母数、空格数、数字数及其他字符数

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;
}

运行结果:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值