18046 字母分类统计

### 思路
1. 读取输入的一行字符。
2. 初始化计数器:字母、数字、空格和其它字符的个数。
3. 遍历每个字符,根据其类型更新相应的计数器。
4. 输出计数结果,格式为:字母、数字、空格和其它字符的个数,中间以空格分隔。

### 伪代码
1. 读取输入的一行字符。
2. 初始化计数器:letters = 0, digits = 0, spaces = 0, others = 0。
3. 遍历每个字符:
   - 如果是字母,letters += 1。
   - 如果是数字,digits += 1。
   - 如果是空格,spaces += 1。
   - 否则,others += 1。
4. 输出计数结果:letters digits spaces others。

### C++代码
 

#include <iostream>
using namespace std;

int main() {
    string input;
    getline(cin, input);

    int letters = 0, digits = 0, spaces = 0, others = 0;

    for (char ch : input) {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
            letters++;
        } else if (ch >= '0' && ch <= '9') {
            digits++;
        } else if (ch == ' ') {
            spaces++;
        } else {
            others++;
        }
    }

    cout << letters << " " << digits << " " << spaces << " " << others << endl;

    return 0;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值