day1_C++:实现C++风格字符串输出

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

程序代码:

cpp 复制代码
#include <iostream>//标准输入输出流
#include <string.h>//C中字符串相关头文件
using namespace std;

int main()
{
    char str[100];
    int big = 0, small = 0, num = 0, space = 0, other = 0;

    cout << "请输入一个字符串:";
    cin.getline(str, 100);//从标准输入输出流读取字符
    for (int i = 0; str[i] != '\0'; i++)//遍历
    {
            if (str[i] >= 'A' && str[i] <= 'Z')
            {
                big++;
            } else if (str[i] >= 'a' && str[i] <= 'z')
            {
                small++;
            } else if (str[i] >= '0' && str[i] <= '9')
            {
                num++;
            } else if (str[i] == ' ')
            {
                space++;
            }
            else
                other++;
        }
        cout << "大写字母个数:" << big << endl;
        cout << "小写字母个数:" << small << endl;
        cout << "数字个数:" << num << endl;
        cout << "空格个数:" << space << endl;
        cout << "其他字符个数:" << other << endl;

        return 0;
    }

运行结果:

流程图:

相关推荐
0 0 01 小时前
CCF-CSP 39-2 水印检查(watermark)【C++】
c++·算法
plus4s1 小时前
2月15日(78,80,81题)
c++·算法·图论
能源系统预测和优化研究2 小时前
【原创改进代码】考虑碳交易与电网交互波动惩罚的共享储能电站优化配置与调度模型
算法·能源
935962 小时前
机考27 翻译21 单词14
c语言·数据结构·算法
光泽雨2 小时前
C# 中 Assembly 类详解
开发语言·c#
少控科技2 小时前
C#基础训练营 - 02 - 运算器
开发语言·c#
Riemann~~3 小时前
C语言嵌入式风格
c语言·开发语言
回敲代码的猴子3 小时前
2月14日打卡
算法
blackicexs4 小时前
第四周第七天
算法
期末考复习中,蓝桥杯都没时间学了4 小时前
力扣刷题19
算法·leetcode·职场和发展