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

描述

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

数据范围:输入的字符串长度满足 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;
}
相关推荐
橘子汽水16817 小时前
Leetcode 128,49最长连续序列,字母异位词分组
java·算法·leetcode
mmmmath_317 小时前
LeetCode.438.找到字符串中所有字母异位词
数据结构·算法·leetcode
Nil20818 小时前
leetcode 22括号生成
算法·leetcode·深度优先
Navigator_Z18 小时前
LeetCode //C - 1237. Find Positive Integer Solution for a Given Equation
c语言·算法·leetcode
政企项目老覃1 天前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水1 天前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR1 天前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵1 天前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽1 天前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法