2024.3.10 C++

提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数要求使用C++风格字符串完成

cpp 复制代码
#include <iostream>
 
using namespace std;
 
int main()
{
 
    char str[20];
    cout << "please enter the str:";
    gets(str);
 
    int uppcaseCount = 0; //定义大写字母的个数
    int lowcaseCount = 0; //定义小写字母的个数
    int digitCount = 0;   //定义数字的个数
    int spaceCount = 0;   //定义空格的个数
    int otherCount = 0;    //定义其他字符的个数
 
    for(int i=0; str[i]!='\0';i++)
    {
        if(str[i]>=65 && str[i]<=90)
        {
            uppcaseCount++;
        }
        else if(str[i]>=97 && str[i]<=122)
        {
            lowcaseCount++;
        }
        else if(str[i]>='1' && str[i]<='9')
        {
            digitCount++;
        }
        else if(str[i] == ' ')
        {
            spaceCount++;
        }
        else
        {
            otherCount++;
        }
    }
    cout << "Uppercase letter:" << uppcaseCount << endl;
    cout << "Lowercase letter:" << lowcaseCount << endl;
    cout << "Digits:" << digitCount << endl;
    cout << "Space:" << spaceCount << endl;
    cout << "Ohter characters" << otherCount << endl;
 
 
    return 0;
}

思维导图

相关推荐
Tyler_TXZ3 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
不会代码的小猴3 小时前
C++新增关键字
开发语言·c++·笔记
liulilittle4 小时前
并发与内存安全术语:定义、分类与关联
c++·安全·并发·术语
_wyt0014 小时前
从图到树:9道洛谷基础题带你入门树形结构
c++·
Titan20245 小时前
Linux网络基础知识
linux·服务器·网络·c++·学习
Titan20246 小时前
Linux网络学习:套接字、UDP的封装与应用
linux·服务器·开发语言·网络·c++
Felven7 小时前
C2. Potions (Hard Version)
c++
Tyler_TXZ8 小时前
C++C语言之——树
c语言·c++·算法·
hansang_IR8 小时前
【记录】「COCI 2024/2025」四道模拟赛/8.13
c++·算法
旖旎夜光8 小时前
LeetCode 30:串联所有单词的子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口