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

描述

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

数据范围:输入的字符串长度满足 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;
}
相关推荐
想吃火锅10057 小时前
【leetcode】56.合并区间js
算法·leetcode·职场和发展
imuliuliang7 小时前
可合并堆在多任务调度中的优势与实现技巧7
算法
学究天人7 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷6)
网络·算法·数学建模·动态规划·几何学·图论·拓扑学
wabs6668 小时前
关于动态规划【力扣72.编辑距离的思考】
算法·leetcode·动态规划
用户333239768848 小时前
我做了一个 RepoMind:让 AI 写架构前,先去看看真实开源项目
算法
chh5638 小时前
C++--list
开发语言·数据结构·c++·学习·算法·list
killerbasd8 小时前
总结 7。10
人工智能·算法·机器学习
林间码客8 小时前
RAG系统评估指南:从入门到实践
人工智能·算法·机器学习
凌波粒8 小时前
LeetCode--47.全排列 II(回溯算法)
算法·leetcode·职场和发展
学究天人8 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷8)
线性代数·算法·机器学习·数学建模·动态规划·图论·抽象代数