c++关于字符串的练习

提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数

cpp 复制代码
#include <iostream>
#include<string>
using namespace std;

int main()
{
    string s1;
    int letter=0,digit=0,space=0,other=0;
    cout<<"请输入一个字符串:";
    getline(cin,s1);
    for(int i=0;i<s1.length();i++)
    {
        char ch=s1[i];
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
        {
            letter++;
        }
        else if(ch>='0'&&ch<='9')
        {
            digit++;
        }
        else if(ch==' ')
        {
            space++;
        }
        else
        {
            other++;
        }

    }
    cout<<"字符串中字母的个数为:"<<letter<<"数字的个数为:"<<digit<<"空格的个数为:"<<space<<"其他字符的个数为:"<<other<<endl;
    return 0;
}

思维导图

相关推荐
ps酷教程5 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云5 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
cjhbachelor5 小时前
c++继承
c++
AI玫瑰助手6 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车6 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋6 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒6 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠6 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨6 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝7 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码