c++day1

提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数

要求使用C++风格字符串完成

cpp 复制代码
#include <iostream>

using namespace std;

int main()
{
    string str;
    cout << "请输入一个含有大小写字母,空格,特殊字符的字符串:" << endl;
    getline(cin,str);//getline可以输入含有带空格的字符串
    int size=str.size();
    int big=0,small=0,a=0,k=0,s=0;
    for (int i=0;i<size;i++)
    {
        if (str.at(i)>='A' && str.at(i)<='Z')
        {
            big++;
        }
        else if (str.at(i)>='a' && str.at(i)<='z')
        {
            small++;
        }
        else if (str.at(i)>='0' && str.at(i)<='9')
        {
            a++;
        }
        else if (str.at(i)==' ')
        {
            k++;
        }
        else
        {
            s++;
        }
    }
    cout << "大写字母=" << big << endl;
    cout << "小写字母=" << small << endl;
    cout << "数字=" << a << endl;
    cout << "空格=" << k << endl;
    cout << "特殊字符=" << s<< endl;
    return 0;
}
相关推荐
沐怡旸6 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4169 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥11 小时前
C++ std::set
c++
侃侃_天下12 小时前
最终的信号类
开发语言·c++·算法
博笙困了12 小时前
AcWing学习——差分
c++·算法
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥13 小时前
C++ std::vector
c++
zl_dfq13 小时前
C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
c++
每天回答3个问题13 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5