华清远见作业第三十二天——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;
}

运行效果:

相关推荐
·present·2 小时前
CSAPP(lecture 4_01)
c语言·c++
FPGA小迷弟2 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
程序员-King.3 小时前
【ROS1】Noetic 开发工作流程与常用命令
c++·ubuntu·ros
云深麋鹿3 小时前
C++ | 基于红黑树实现mymap与myset
开发语言·c++
傲世仙尊5 小时前
string增删查改-insert-erase-replace与find加substr组合技
c++
·present·6 小时前
CSAPP(lecture 3)
c++
别动我齐刘海6 小时前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
basketball6167 小时前
C++ 中字符串与常见类型的转换总结
开发语言·c++
等我调个Bug7 小时前
C++智能指针通俗讲解
开发语言·c++
kyle~9 小时前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器