C++day1

一、思维导图

二、提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数

cpp 复制代码
#include <iostream>

using namespace std;

int main()
{
    while(1)
    {
        int letter = 0;
        int number =0;
        int space = 0;
        int other = 0;

        string str;
        cout << "请输入字符串:";
        getchar();
        getline(cin,str);
        for(int i=0;i<str.length();i++)
        {
            char c = str[i];
            //字母
            if((c >= 'A' && c <= 'Z') ||(c >= 'a' && c <= 'z'))
            {
                letter++;
            }
            //数字
            else if(c >= '0' && c <='9')
            {
                number++;
            }
            //空格
            else if(c == ' ')
            {
                space++;
            }
            else
                other++;
        }
        cout<<"字母:"<<letter<<endl<<"数字:"<<number<<endl<<"空格:"<<space<<endl<<"其它:"<<other<<endl;
    }

    return 0;
}
相关推荐
铉铉这波能秀1 分钟前
LeetCode Hot100 中 enumerate 函数的妙用(2026.2月版)
数据结构·python·算法·leetcode·职场和发展·开发
墨有6668 分钟前
哈希表从入门到实现,一篇吃透!
数据结构·算法·哈希算法
Yu_Lijing10 分钟前
网络复习篇——网络基础(一)
网络·c++·笔记
Bella的成长园地11 分钟前
为什么c++中的条件变量的 wait() 函数需要配合while 循环或谓词?
c++·面试
We་ct12 分钟前
LeetCode 228. 汇总区间:解题思路+代码详解
前端·算法·leetcode·typescript
charlee4413 分钟前
为什么现代 C++ 库都用 PIMPL?一场关于封装、依赖与安全的演进
c++·智能指针·raii·pimpl·编译防火墙·封装设计
AIpanda88817 分钟前
如何借助AI销冠系统提升数字员工在销售中的成效?
算法
啊阿狸不会拉杆18 分钟前
《机器学习导论》第 7 章-聚类
数据结构·人工智能·python·算法·机器学习·数据挖掘·聚类
木非哲23 分钟前
机器学习--从“三个臭皮匠”到 XGBoost:揭秘 Boosting 算法的“填坑”艺术
算法·机器学习·boosting
MSTcheng.23 分钟前
CANN ops-math算子的跨平台适配与硬件抽象层设计
c++·mfc