华清远见作业第三十二天——C++(第一天)

思维导图:

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

代码:

cpp 复制代码
#include <iostream>
#include<array>
using namespace std;

int main()
{
    string str;
    cout << "请输入一个字符串:" ;
    getline(cin,str);
    int line=(str.size());//记录有多长
    int big_num=0;//大写个数
    int small_num=0;//小写个数
    int num_num=0; //数字个数
    int empty_num=0;//空格个数
    int other_num=0;//其他个数
    for(int i=0;i<line;i++)
    {
        if(str[i]>='A'&&str[i]<='Z')//大写字母
        {
            big_num++;
        }else if(str[i]>='a'&&str[i]<='z')//小写字母
        {
            small_num++;
        }else if(str[i]>='0' && str[i]<='9' )//数字个数
        {
            num_num++;
        }else if(str[i]==' ')//空格
        {
            empty_num++;
        }else//其他符号
        {
            other_num++;
        }
    }
    cout << "这个字符串总共有" << line << "个字符" << endl;
    cout << "big_num=" << big_num << endl;
    cout << "small_num=" << small_num << endl;
    cout << "num_num=" << num_num << endl;
    cout << "empty_num=" << empty_num << endl;
    cout << "other_num=" << other_num << endl;
    return 0;
}

运行效果:

相关推荐
ajassi20002 小时前
开源 C++ QT QML 开发(二十)多媒体--摄像头拍照
c++·qt·开源
_OP_CHEN2 小时前
C++基础:(十二)list类的基础使用
开发语言·数据结构·c++·stl·list类·list核心接口·list底层原理
晚风残5 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
满天星83035776 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
Mr_WangAndy6 小时前
C++设计模式_行为型模式_责任链模式Chain of Responsibility
c++·设计模式·责任链模式·行为型模式
时间之里7 小时前
【c++】:Lambda 表达式介绍和使用
开发语言·c++
汉克老师7 小时前
GESP2025年9月认证C++四级( 第三部分编程题(1)排兵布阵)
c++·算法·gesp4级·gesp四级
·心猿意码·8 小时前
C++智能指针解析
开发语言·c++
property-9 小时前
C++中#define和const的区别
开发语言·c++
怎么没有名字注册了啊10 小时前
查找成绩(数组实现)
c++·算法