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

相关推荐
m0_6948455716 小时前
教你使用服务器如何搭建数据库
linux·运维·服务器·数据库·云计算
青草地溪水旁16 小时前
设计模式(C++)详解——建造者模式(2)
c++·设计模式·建造者模式
空灵之海16 小时前
Ubuntu Server 22.04.5系统安装教程
linux·运维·ubuntu
郝学胜-神的一滴16 小时前
深入探索 C++ 元组:从基础到高级应用
开发语言·c++·stl·软件工程
gamers16 小时前
rock linux 9 安装mysql 5.7.44
linux·mysql·adb
量子炒饭大师17 小时前
收集飞花令碎片——C语言关键字typedef
c语言·c++·算法
二进制_博客17 小时前
给CentOS的虚拟机扩容
linux·运维·centos
千钰v17 小时前
Tcpdump: The Basics Tcpdump 基础
linux·运维·网络·tcpdump·tryhackme
kk57917 小时前
【Ubuntu】sudo apt update出现E :仓库***没有Release文件
linux·运维·ubuntu
cellurw17 小时前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http