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;
}
相关推荐
A1-2911 分钟前
C++的四种类型转换
开发语言·c++
噜啦噜啦嘞好1 小时前
c++的特性——多态
开发语言·c++
ydm_ymz1 小时前
初阶8 list
c语言·开发语言·数据结构·c++·list
Java版蜡笔小新2 小时前
算法-贪心算法
算法·贪心算法
LuckyAnJo2 小时前
Leetcode-100 回溯法-电话号码的字母组合
python·算法·leetcode
HR Zhou2 小时前
群体智能优化算法-鹈鹕优化算法(Pelican Optimization Algorithm, POA,含Matlab源代码)
算法·机器学习·matlab·优化·群体智能优化
钰爱&2 小时前
【Qt】ffmpeg编码—存储(H264)
c++·qt·ffmpeg
LuckyLay2 小时前
LeetCode算法题(Go语言实现)_20
算法·leetcode·职场和发展·golang
想要成为计算机高手2 小时前
12. STL的原理
开发语言·c++·经验分享·stl
Heisenberg~3 小时前
C++ 多态:面向对象编程的核心概念(二)
c++