北林oj题目代码C++用函数实现统计输入字符串英文单词的个数和特定标点符号的数量

Reading papers

描述

 

论文 "Fair Allocation of Scarce Medical Resources in the Time of Covid-19" 探讨了Covid-19疫情中稀缺医疗资源分配的公平性问题。现摘取其中一段,请使用C++字符串流对其进行分析,完成以下功能:

  • 统计这段文字的单词总数(以空格分隔为准)
  • 统计这段文字的标点总数(仅考虑半角句号、逗号、双引号)

注意本题要求:

  • main函数已写好如下,只提交readPapers()函数
  • 头文件需由自己包含
int main() {
    std::string content;
    std::getline(std::cin, content, '\n');
    readPapers(content);
    return 0;
}

输入

 

一段不换行、含句号、逗号、双引号的英文文字。

输出

 

分别输出单词总数和标点总数,如

30,6

输入样例 1 

No matter what difficulties we encounter, don't be afraid and face them with a smile. The best way to eliminate fear is to face the fear itself. "Persistence is victory".

输出样例 1

30,6

提示

  • 根据英文书写规则,标点符号与单词之间可能没有空格。
  • 双引号为 " "

代码:

#include<iostream>
#include<string>
using namespace std;
void readPapers(string content)
{
    int count = 0;
    int i = 0;
    int j = 0;
    while (i <= content.length())
    {
        if (content[i] == ' ')
            count++;
        if ((content[i] == ',') || (content[i] == '.') || (content[i] == '"'))
            j++;
        i++;
    }
    count++;
    cout << count << "," << j << endl;
}
int main()
{
    std::string content;
    std::getline(std::cin, content, '\n');
    readPapers(content);
    return 0;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值