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;
}
相关推荐
爪哇部落算法小助手13 分钟前
每日两题day50
数据结构·c++·算法
curry____3031 小时前
基本算法(2025.11.21)
c++·算法
钟屿1 小时前
Back to Basics: Let Denoising Generative Models Denoise 论文阅读学习
论文阅读·人工智能·笔记·学习·计算机视觉
d111111111d1 小时前
SPI通信协议--在STM32中介绍(学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习
tan180°1 小时前
Linux网络TCP(上)(11)
linux·网络·c++·后端·tcp/ip
WWZZ20252 小时前
快速上手大模型:深度学习5(实践:过、欠拟合)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
断水客2 小时前
如何在手机上搭建Linux学习环境
linux·运维·学习
司铭鸿2 小时前
图论中的协同寻径:如何找到最小带权子图实现双源共达?
linux·前端·数据结构·数据库·算法·图论
橘子真甜~2 小时前
C/C++ Linux网络编程6 - poll解决客户端并发连接问题
服务器·c语言·开发语言·网络·c++·poll
铭哥的编程日记2 小时前
【标准项目】C++基于正倒排索引的Boost搜索引擎
c++·搜索引擎