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;
}
相关推荐
皓月斯语14 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet14 小时前
树状结构在查询优化中的作用与实现细节7
算法
徐凤年_15 小时前
rog_map参数理解
算法
春日见15 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
萌动的小火苗15 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索15 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana216 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
星恒随风16 小时前
C++ STL 栈详解:stack 的使用、经典题目与简单模拟实现
开发语言·数据结构·c++·笔记·学习
txzrxz16 小时前
单调队列讲解
数据结构·c++·算法·单调队列
WWTYYDS_66617 小时前
JsonCpp超详细使用教程
c++