2024.3.10 C++

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

cpp 复制代码
#include <iostream>
 
using namespace std;
 
int main()
{
 
    char str[20];
    cout << "please enter the str:";
    gets(str);
 
    int uppcaseCount = 0; //定义大写字母的个数
    int lowcaseCount = 0; //定义小写字母的个数
    int digitCount = 0;   //定义数字的个数
    int spaceCount = 0;   //定义空格的个数
    int otherCount = 0;    //定义其他字符的个数
 
    for(int i=0; str[i]!='\0';i++)
    {
        if(str[i]>=65 && str[i]<=90)
        {
            uppcaseCount++;
        }
        else if(str[i]>=97 && str[i]<=122)
        {
            lowcaseCount++;
        }
        else if(str[i]>='1' && str[i]<='9')
        {
            digitCount++;
        }
        else if(str[i] == ' ')
        {
            spaceCount++;
        }
        else
        {
            otherCount++;
        }
    }
    cout << "Uppercase letter:" << uppcaseCount << endl;
    cout << "Lowercase letter:" << lowcaseCount << endl;
    cout << "Digits:" << digitCount << endl;
    cout << "Space:" << spaceCount << endl;
    cout << "Ohter characters" << otherCount << endl;
 
 
    return 0;
}

思维导图

相关推荐
cany10005 小时前
C++ -- 可变参数模板
c++
不会C语言的男孩6 小时前
C++ Primer 第2章:变量和基本类型
开发语言·c++
云泽8088 小时前
C++ 可调用对象通关指南:深度解析 Lambda 表达式、function 包装器与 bind 绑定器
开发语言·c++·算法
Tri_Function8 小时前
简单图论大学习
c++
lqqjuly9 小时前
C++ 完整知识体系—从基础语法到现代 C++23 的系统性总结
c++·c++23
王老师青少年编程9 小时前
信奥赛C++提高组csp-s之FHQ Treap
c++·csp·平衡树·信奥赛·csp-s·提高组·fhq treap
QiLinkOS11 小时前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
Irissgwe11 小时前
c++STL--string类
c++·stl·string
Irissgwe11 小时前
c++类型转换
c++·类型转换·explicit·static_cast·const_cast·dynamic_cast·rtti
智者知已应修善业11 小时前
【51单片机用T0定时器方式1,实现0.5S的时间间隔实现第一次一个灯亮、第二次二个灯亮,直到全部灯亮,然后重复整个过程】2023-12-29
c++·经验分享·笔记·算法·51单片机