坐牢第三十天(c++)

1.作业:

提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数

复制代码
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
    string str;
    cout << "请输入一个字符串:"; 
    getline(cin,str);
    int len = str.length();//字符串实际长度
    cout << "字符串的长度为:";
    cout << len << endl;
    int alphabet=0;//字母个数
    int number=0;//数字个数
    int space=0;//空格个数
    int other=0;//其他字符个数
    for (int  i = 0; i < len; i++)
    {
        if (str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
        alphabet++;
        else if(str[i]>='0'&&str[i]<='9')
        number++;
        else if(str[i]==' ')
        space++;
        else
        other++;
    }
    cout << "字母有:"<< alphabet <<endl;
    cout << "数字有:"<< number <<endl;
    cout << "空格有:"<< space <<endl;
    cout << "其他字符有:"<< other <<endl;
    return 0;
}

效果图:

2.思维导图:

相关推荐
乱蜂朝王4 小时前
Ubuntu 20.04安装CUDA 11.8
linux·运维·ubuntu
君义_noip4 小时前
信息学奥赛一本通 1661:有趣的数列 | 洛谷 P3200 [HNOI2009] 有趣的数列
c++·算法·组合数学·信息学奥赛·csp-s
梁洪飞5 小时前
clk学习
linux·arm开发·嵌入式硬件·arm
~光~~6 小时前
【嵌入式linux驱动——点亮led】基于鲁班猫4 rk3588s
linux·点灯·嵌入式linux驱动
hele_two6 小时前
快速幂算法
c++·python·算法
OopspoO6 小时前
C++杂记——Name Mangling
c++
yuanmenghao6 小时前
车载Linux 系统问题定位方法论与实战系列 - 车载 Linux 平台问题定位规范
linux·运维·服务器·网络·c++
小羊羊Python6 小时前
SoundMaze v1.0.1正式发布!
开发语言·c++
qq_589568107 小时前
centos6.8镜像源yum install不成功,无法通过镜像源下载的解决方式
linux·运维·centos
weixin_516023078 小时前
linux下fcitx5拼音的安装
linux·运维·服务器