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

相关推荐
让我们一起加油好吗2 小时前
【基础算法】初识搜索:递归型枚举与回溯剪枝
c++·算法·剪枝·回溯·洛谷·搜索
郝学胜-神的一滴2 小时前
Horse3D游戏引擎研发笔记(七):在QtOpenGL环境下,使用改进的Uniform变量管理方式绘制多彩四边形
c++·3d·unity·游戏引擎·图形渲染·虚幻·unreal engine
·云扬·3 小时前
从零开始搭 Linux 环境:VMware 下 CentOS 7 的安装与配置全流程(附图解)
linux·运维·centos
2401_876221344 小时前
Reachability Query(Union-Find)
c++·算法
躲着人群6 小时前
次短路&&P2865 [USACO06NOV] Roadblocks G题解
c语言·数据结构·c++·算法·dijkstra·次短路
btyzadt7 小时前
虚拟机蓝屏问题排查与解决
linux·运维·网络
一只鲲7 小时前
56 C++ 现代C++编程艺术5-万能引用
开发语言·c++
G_H_S_3_8 小时前
【网络运维】Shell 脚本编程:while 循环与 until 循环
linux·运维·网络·shell
小欣加油8 小时前
leetcode 1493 删掉一个元素以后全为1的最长子数组
c++·算法·leetcode
coderklaus8 小时前
Shell 基础知识
linux·macos·shell