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 小时前
DeepSeek V4 和 GPT-5.5 在同一天发布了??我也很懵,但对比完我悟了
算法
DeepModel2 小时前
通俗易懂讲透 Q-Learning:从零学会强化学习核心算法
人工智能·学习·算法·机器学习
田梓燊2 小时前
力扣:19.删除链表的倒数第 N 个结点
算法·leetcode·链表
handler013 小时前
从零实现自动化构建:Linux Makefile 完全指南
linux·c++·笔记·学习·自动化
安小牛3 小时前
Android 开发汉字转带声调的拼音
android·java·学习·android studio
简简单单做算法3 小时前
基于GA遗传优化双BP神经网络的时间序列预测算法matlab仿真
神经网络·算法·matlab·时间序列预测·双bp神经网络
guygg884 小时前
利用遗传算法解决列车优化运行问题的MATLAB实现
开发语言·算法·matlab
武藤一雄4 小时前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
sali-tec4 小时前
C# 基于OpenCv的视觉工作流-章52-交点查找
图像处理·人工智能·opencv·算法·计算机视觉
仙女修炼史4 小时前
CNN的捷径学习Shortcut Learning in Deep Neural Networks
人工智能·学习·cnn