统计数字、字母、空格、其他字符练习题

描述

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

数据范围:输入的字符串长度满足 1≤n≤1000 1≤n≤1000

输入描述:

输入一行字符串,可以有空格

输出描述:

统计其中英文字符,空格字符,数字字符,其他字符的个数

示例1

输入:

复制代码
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

输出:

复制代码
26
3
10
12
复制代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    getline(cin,str);
    int a=0;
    int b=0;
    int c=0;
    int d=0;
    for(int i=0;i<str.size();i++)
    {
        if(isalpha(str[i]))
        {
            a++;
        }
        else if(str[i]==' ')
        {
            b++;
        }
        //else if(str[i]>=0&&str[i]<=9)//
        else if(str[i]>='0'&&str[i]<='9')
        {
            c++;
        }
        else
        {
            d++;

        }
    }
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c<<endl;
    cout<<d<<endl;
}
相关推荐
土司大王25 分钟前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背2 小时前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
zander2582 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
祖力553 小时前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜3 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者3 小时前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法
_Narcissus_3 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.3 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
threerocks4 小时前
《The AI-Native SDLC Playbook》万字拆解
算法·aigc·ai编程
夏玉林的学习之路4 小时前
算法8.环形队列
算法