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

思维导图

相关推荐
zfoo-framework5 分钟前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
天佑木枫10 分钟前
15天Python入门系列 · 序
开发语言·python
逍遥德25 分钟前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD30 分钟前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring
weixin_5231853232 分钟前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
点燃大海34 分钟前
SpringAI构建智能体
java·spring boot·spring·springai智能体
xier_ran36 分钟前
【infra之路】02_RadixAttention与KV_Cache管理
java·spring boot·spring
黑马师兄1 小时前
RAG混合检索深度解析:让AI真正找到你要的内容
java·人工智能·ai·agent·rag·ai-native
码客日记1 小时前
Spring Boot 配置文件敏感信息加密(Jasypt 企业级完整方案)
java·spring boot·git
宋拾壹1 小时前
同时添加多个类目
android·开发语言·javascript