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

运行结果:

相关推荐
铅笔侠_小龙虾13 小时前
Rust 学习(4)-函数与注释
开发语言·学习·rust
ysa05103013 小时前
【板子】拓扑排序
c++·算法·图论·板子
tiana_13 小时前
写了个零依赖的 Go 版本管理器,curl | bash 完事
开发语言·golang·bash
嘘嘘出差14 小时前
Python 爬虫入门实战:爬取豆瓣电影 Top 250
开发语言·爬虫·python
爱写代码的倒霉蛋14 小时前
实现协程的三种方式
开发语言·python
某不知名網友14 小时前
项目:轻量级搜索引擎
c++
CS创新实验室14 小时前
NumPy数组的C风格和Fortran风格
c语言·开发语言·numpy
逝水无殇15 小时前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博15 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
前端H15 小时前
Biome & Rolldown:Rust 工具链接管前端基建
开发语言·前端·rust