Wells


私信TA

用户名:3180624024

访问量:10016

签 名:

等  级
排  名 2598
经  验 2144
参赛次数 0
文章发表 7
年  龄 0
在职情况 学生
学  校 渣渣大学
专  业 大数据

  自我简介:

TA的其他文章

解题思路:

没什么技巧,主要是注意cin在输入string时的注意事项
注意事项:

如果单纯的使用:cin >> str

在遇到空格、回车时会自动结束输入

则可以使用getline()函数

用法:接收一个字符串,可以接收空格并输出,需包含“#include<string>”

参考代码:

#include<iostream>
#include<string>
using namespace std;
void count(string str)
{
	int al = 0, num = 0, bl = 0, el = 0;
	for (char ch : str) {
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
            al++;
        else if (ch >= '0' && ch <= '9')
            num++;
        else if (ch == ' ')
            bl++;
        else
            el++;
	}
    cout << al << ' ' << num << ' ' << bl << ' ' << el << endl;
}

int main()
{
    string str;
    getline(cin, str);
    count(str);
    return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区