题目描述
给出一段字符,请统计这段字符有几行,几个单词和几个字符。
定义单词为用空格或者换行符隔开的连续字符
字符定义为包括一般可见字符以及空格
输入描述:
第一行一个正整数 T ,代表测试数据的组数
第二行开始为第一组测试数据,测试数据每行不超过1024个字符
每两组测试数据之间用连续的 5 个 = 分隔,保证测试数据中不会出现连续的 5 个 =
输出描述:
每组测试数据在一行中输出由空格分隔的3个整数,分别代表行数,单词数和字符数
示例1
输入
2
This is a sample input.
Hello World!!
=====
The speech by Hunyak, translated, is:
"What am I doing here?
They say, the famous Hungarian police,
that I held down my husband and chopped off his head.
But I didn't do it, I am not guilty.
I can't believe that Uncle Sam says I did it.
They say I didit, but really I didn't."
输出
2 7 37
8 55 270
说明 对于第一个样例,有2行7个单词是显而易见的 有28个字母,3个标点符号以及6个空格,所以共有37个字符 请注意换行符不算在字符个数里
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int t;
cin >> t;
getchar();
while(t--)
{
string str;
int cnt = 0, a = 0, b = 0;
while(getline(cin, str))//读入一行包含空格的字符串
{
if(str == "=====") break;
b += str.size();//统计字符的个数
stringstream ss;
string p;
ss << str;//向流中传入值
while(ss >> p) a++;//读取每一个子串,并统计单词的个数
cnt++;//统计读入的行数
}
cout << cnt << ' ' << a << ' ' << b << endl;
}
return 0;
}