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;
}
相关推荐
墨染点香12 分钟前
LeetCode 刷题【126. 单词接龙 II】
算法·leetcode·职场和发展
aloha_78936 分钟前
力扣hot100做题整理91-100
数据结构·算法·leetcode
Tiny番茄41 分钟前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的41 分钟前
最长连续序列
数据结构·c++·算法
前端小L2 小时前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
future14122 小时前
MCU硬件学习
单片机·嵌入式硬件·学习
好奇龙猫2 小时前
日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(4):文法運用
学习
RTC老炮2 小时前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
mjhcsp2 小时前
C++ int 类型深度解析:从底层实现到实战应用
c++·int
21号 12 小时前
9.Redis 集群(重在理解)
数据库·redis·算法