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

相关推荐
DYWorker0012 分钟前
第1课:基于正点原子RK3568开发板读懂Linux内核
linux·运维·网络
kiracrimson5 分钟前
string 容器:从 C 字符串到会自己扩容的类
开发语言·数据结构·c++·stl
程序猿编码9 分钟前
纯 C++ 手写 LLM 推理引擎:分页 KV 缓存、连续批、多卡张量并行全部内置
c++·深度学习·缓存·大模型推理·自动微分·模型推理
Android系统攻城狮23 分钟前
Linux FFmpeg深度解析之avformat_open_input调用流程与实战(一)
linux·运维·ffmpeg·车载音视频·ffmpeg音视频·音视频进阶手册
G018_star sky♬1 小时前
nodeJS他是如何用JavaScript封装C++的呢
开发语言·javascript·c++
吹什么轩1 小时前
linux系统复习:Ext 文件系统(一个文件在磁盘上到底长什么样)
linux·运维·服务器
吹什么轩1 小时前
linux系统复习:全景串联
linux·运维·服务器
m0_547486661 小时前
《Ubuntu Linux操作系统与实验教程》全套PPT课件2026
linux·ubuntu·操作系统
choumin1 小时前
C++中std::move和std::forward的关系和区别
c++·std move·std forward·关系和区别
John_Hui1 小时前
第 1 篇:VMware 安装 Ubuntu 24.04.3 Server
linux·运维·ubuntu