C++初学

1>思维导图

2>试编程

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

cpp 复制代码
#include <iostream>
#include<string.h>
using namespace std;

int main()
{
    string str;
    cout << "please enter str:";
    getline(cin,str);
    int len = str.size();
    int cap = 0;//大写字母
    int low = 0;//小写字母
    int spa = 0;//空格
    int num = 0;//数字
    int oth = 0;//其他
    for(int i=0;i<len;i++)
    {
        if(str[i] >= 'a' && str[i] <= 'z')
        {
            low++;
        }
        else if(str[i] >= 'A' && str[i] <= 'Z')
        {
            cap++;
        }
        else if(str[i] >= '0' && str[i] <= '9')
        {
            num++;
        }
        else if(str[i] == ' ')
        {
            spa++;
        }
        else
        {
            oth++;
        }
    }
    cout << "cap=" << cap << endl;
    cout << "low=" << low << endl;
    cout << "num=" << num << endl;
    cout << "oth=" << oth << endl;
    cout << "spa=" << spa << endl;

    return 0;
}

运行结果:

相关推荐
大圣编程10 分钟前
Python中continue语句的用法是什么?
开发语言·前端·python
upgrador37 分钟前
基础知识:C++ STL构造函数的左闭右开惯例及其实现原理
开发语言·c++
yoothey2 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash
geovindu2 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
wuyk5552 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
凯瑟琳.奥古斯特3 小时前
K次取反最大化数组和解法(力扣1005)
开发语言·c++·算法·leetcode·职场和发展
林中青木3 小时前
CT重构原理及C++代码实现
c++·计算机视觉·重构
AC赳赳老秦3 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw
满天星83035773 小时前
Protobuf的介绍及使用
c++
☆cwlulu3 小时前
调试排查工具介绍(gdb、strace、Valgrind等)
开发语言·c++·嵌入式硬件·ubuntu