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

运行效果:

相关推荐
博客18001 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴1 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨2 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4566 天前
C++进阶(1)——前景提要
c++
夜悊6 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴6 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0017 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾7 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you7 天前
constexpr函数
c++
凡人叶枫7 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++