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

运行结果:

相关推荐
王燕龙(大卫)14 小时前
linuxptp时间同步
c++
郝学胜-神的一滴14 小时前
深入理解Linux套接字(Socket)编程:从原理到实践
linux·服务器·开发语言·网络·c++·程序人生·算法
程序猿编码14 小时前
高性能HTTP服务压测工具:设计思路与实现原理(C/C++代码实现)
c语言·网络·c++·网络协议·tcp/ip·http
向前V14 小时前
Flutter for OpenHarmony轻量级开源记事本App实战:笔记编辑器
开发语言·笔记·python·flutter·游戏·开源·编辑器
时艰.14 小时前
JVM — Java 类加载机制
java·开发语言·jvm
2301_8035545215 小时前
c++hpc岗位
c++
坐怀不乱杯魂15 小时前
Linux - 线程
linux·c++
小小码农Come on15 小时前
QT中窗口位置、相对位置、绝对位置
android·开发语言·qt
diediedei15 小时前
C++中的适配器模式变体
开发语言·c++·算法
郝学胜-神的一滴15 小时前
Python中的Mixin继承:灵活组合功能的强大模式
开发语言·python·程序人生