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;
}