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;
}

思维导图

相关推荐
Tony Bai2 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
wjs20242 小时前
Swift 类型转换
开发语言
没有bug.的程序员2 小时前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
一线大码3 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
秃了也弱了。3 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730503 小时前
java数组整理笔记
java·开发语言·笔记
weixin_425023003 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
一线大码3 小时前
Java 8-25 各个版本新特性总结
java·后端
Thera7773 小时前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
2501_906150563 小时前
私有部署问卷系统操作实战记录-DWSurvey
java·运维·服务器·spring·开源