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;
}
相关推荐
Code Warrior1 小时前
【每日算法】专题五_位运算
开发语言·c++
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
Coding小公仔7 小时前
C++ bitset 模板类
开发语言·c++
菜鸟看点8 小时前
自定义Cereal XML输出容器节点
c++·qt
悲伤小伞9 小时前
linux_git的使用
linux·c语言·c++·git
ysa0510309 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
小小小小王王王12 小时前
求猪肉价格最大值
数据结构·c++·算法
岁忧13 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
码农编程录14 小时前
【c/c++3】类和对象,vector容器,类继承和多态,systemd,std&boost
c++
??tobenewyorker15 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode