C++初学

1>思维导图

2>试编程

提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数要求使用C++风格字符串完成

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

int main()
{
    string str;
    cout << "please enter str:";
    getline(cin,str);
    int len = str.size();
    int cap = 0;//大写字母
    int low = 0;//小写字母
    int spa = 0;//空格
    int num = 0;//数字
    int oth = 0;//其他
    for(int i=0;i<len;i++)
    {
        if(str[i] >= 'a' && str[i] <= 'z')
        {
            low++;
        }
        else if(str[i] >= 'A' && str[i] <= 'Z')
        {
            cap++;
        }
        else if(str[i] >= '0' && str[i] <= '9')
        {
            num++;
        }
        else if(str[i] == ' ')
        {
            spa++;
        }
        else
        {
            oth++;
        }
    }
    cout << "cap=" << cap << endl;
    cout << "low=" << low << endl;
    cout << "num=" << num << endl;
    cout << "oth=" << oth << endl;
    cout << "spa=" << spa << endl;

    return 0;
}

运行结果:

相关推荐
必须会一定会2 小时前
Agent Plugins 1.0实战:plugin.json、skills、mcp.json目录结构与迁移
开发语言·人工智能·ai编程
St_rive2 小时前
Page Object设计模式
java·开发语言·设计模式
wp123_12 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
wuyk5552 小时前
4.树:一对多的层次数据结构
开发语言·数据结构·stm32·单片机
luj_17683 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
会周易的程序员4 小时前
aiDgePLC iec61131 虚拟机 完整使用文档
c++·物联网·架构·st·iec61131
小小龙学IT4 小时前
Boost.Beast 深度实战:基于 Asio 的开源 C++ HTTP/WebSocket 协议库
c++·websocket·http
caimouse5 小时前
ReactOS 图形系统分析(16):字符串对象 — STROBJ(string.c)
c语言·开发语言
Aphelios3805 小时前
一次锁内网络IO引发的Tomcat线程池“饿死”事故
java·开发语言·spring boot·elasticsearch·tomcat·网络io阻塞·线程池耗尽