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;
}
相关推荐
D_evil__6 小时前
【Effective Modern C++】第二章 auto:6. 当auto推导的类型不符合要求时,使用显式类型初始化习惯用法
c++
夏鹏今天学习了吗6 小时前
【LeetCode热题100(87/100)】最小路径和
算法·leetcode·职场和发展
jacGJ7 小时前
记录学习--文件读写
java·前端·学习
哈哈不让取名字7 小时前
基于C++的爬虫框架
开发语言·c++·算法
枷锁—sha7 小时前
【PortSwigger Academy】SQL 注入绕过登录 (Login Bypass)
数据库·sql·学习·安全·网络安全
魔芋红茶8 小时前
Spring Security 学习笔记 2:架构
笔记·学习·spring
Lips6119 小时前
2026.1.20力扣刷题笔记
笔记·算法·leetcode
2501_941329729 小时前
YOLOv8-LADH马匹检测识别算法详解与实现
算法·yolo·目标跟踪
洛生&9 小时前
Planets Queries II(倍增,基环内向森林)
算法
剑锋所指,所向披靡!9 小时前
C++之类模版
java·jvm·c++