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

这段C++代码片段用于接收用户输入的字符串,统计其中大写、小写字母、数字、空格和其他字符的数量,展示了基本的字符处理和计数功能。
摘要由CSDN通过智能技术生成

思维导图:

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

代码:

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

运行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值