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;
}
相关推荐
西西弗Sisyphus1 分钟前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 指针与内存地址(2)
开发语言·c++
16月6日-晴23 分钟前
斐波那契数列题解
算法·leetcode
郝学胜-神的一滴1 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
西西弗Sisyphus2 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - typedef 封装(4)
c++
笑鸿的学习笔记2 小时前
C++笔记之信号量和引用计数的概念是否等价?
java·c++·笔记
Keven_112 小时前
算法札记:有限差分
算法
忙里偷闲学python2 小时前
GK2A 林火蔓延预报算法说明
算法
我是伪码农2 小时前
11.21程序设计
数据结构·算法
Tisfy3 小时前
LeetCode 3561.移除相邻字符:栈(相邻相消)
算法·leetcode·字符串·题解·栈·相邻相消
战6173 小时前
Python 基础算法:三种排序、二分查找与递归
开发语言·python·算法