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;
}
相关推荐
码匠许师傅5 小时前
【C++ 面试真题】C++ 的 const 和 constexpr 有什么区别?
c++·面试
坚持编程的菜鸟7 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
wabs6668 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
云深处@8 小时前
【数据库】MySQL 入门
数据库·学习·mysql
果果燕8 小时前
实习笔记(一):NFS、Samba、VNC、Git、CMake、systemd、内核、交叉编译
linux·arm开发·c++·笔记·git
坚持编程的菜鸟8 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou8 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
HyperAI超神经9 小时前
【vLLM 学习】Disaggregated Prefill
人工智能·深度学习·学习·vllm
牛艺翔9 小时前
C++基础
开发语言·c++
键盘会跳舞9 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出