坐牢第三十天(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.思维导图:

相关推荐
SPC的存折25 分钟前
8、Ansible之Playbook---Roles
linux·服务器·ansible
Kira Skyler28 分钟前
BPF KPROBE编程中的ctx是什么?
linux
三万棵雪松2 小时前
【Linux 物联网网关主控系统-Linux主控部分(三)】
linux·物联网·嵌入式linux
萝卜白菜。2 小时前
TongWeb7.0 集中管理heimdall配置文件说明
linux·运维·服务器
是娇娇公主~3 小时前
Lambda表达式详解
数据结构·c++
leaves falling3 小时前
C++ string 类:从入门到模拟实现
开发语言·c++
IMPYLH3 小时前
Linux 的 install 命令
linux·运维·服务器·bash
样例过了就是过了3 小时前
LeetCode热题100 柱状图中最大的矩形
数据结构·c++·算法·leetcode
浦信仿真大讲堂4 小时前
CST FAQ 006:Linux系统CST安装指导
linux·运维·服务器·仿真软件·达索软件
liuyao_xianhui4 小时前
优选算法_最小基因变化_bfs_C++
java·开发语言·数据结构·c++·算法·哈希算法·宽度优先