C++ 基础学习

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

cpp 复制代码
#include <iostream>

using namespace std;

int main()
{
    cout<<"请输入字符串:";
    string str;
    getline(cin,str);
    int num=0;
    int alp=0;
    int spa=0;
    int other=0;
    int len=str.length();
    for(int i=0;i<len;i++)
    {
        if(str[i]>='0'&&str[i]<='9')
        {
            num++;
        }else if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
        {
            alp++;
        }else if(str[i]==' ')
        {
            spa++;
        }else
        {
            other++;
        }
    }
    cout<<"数字"<<num<<"个"<<endl;
    cout<<"字母"<<alp<<"个"<<endl;
    cout<<"空格"<<spa<<"个"<<endl;
    cout<<"其他字符"<<other<<"个"<<endl;
    return 0;
}
相关推荐
十年编程老舅1 天前
冲刺米哈游|游戏开发一面面经(26 届
linux·c++·米哈游
进阶小白猿1 天前
Java技术八股学习Day29
学习
蒟蒻的贤1 天前
滑动窗口策略
算法
闫记康1 天前
linux配置ssh
linux·运维·服务器·学习·ssh
闪电麦坤951 天前
Leecode热题100:矩阵置零(矩阵)
线性代数·算法·矩阵
浅念-1 天前
C语言——双向链表
c语言·数据结构·c++·笔记·学习·算法·链表
轩情吖1 天前
数据结构-图
数据结构·c++·邻接表·邻接矩阵·最小生成树·kruskal算法·prim算法
Wh-Constelltion1 天前
【PQ分解法潮流计算(matlab版)】
算法·matlab
lxl13071 天前
学习C++(5)运算符重载+赋值运算符重载
学习