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;
}
相关推荐
柠檬少少开发8 分钟前
图像拼接算法及实现(一)
人工智能·算法·计算机视觉
weixin_486681141 小时前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言1 小时前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
代码小狗Codog2 小时前
C++独立开发开源大数计算库 CBigNum
数据结构·c++
咕噜咕嘟嘟嘟2 小时前
343. 整数拆分
数据结构·算法
WenGyyyL3 小时前
力扣最热一百题——二叉树的直径
java·c++·算法·二叉树·深度优先
sdlkjaljafdg3 小时前
vector<bool>性能测试
开发语言·c++·算法
telllong3 小时前
使用llama.cpp 在推理MiniCPM-1.2B模型
c++·llama·llama.cpp