1142:单词的长度
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 8157 通过数: 4725
【题目描述】
输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度。
注意:如果有标点符号(如连字符,逗号),标点符号算作与之相连的词的一部分。没有被空格间开的符号串,都算作单词。
【输入】
一行单词序列,最少1个单词,最多300个单词,单词之间用至少1个空格间隔。单词序列总长度不超过1000。
【输出】
依次输出对应单词的长度,之间以逗号间隔。
【输入样例】
She was born in 1990-01-02 and from Beijing city.
【输出样例】
3,3,4,2,10,3,4,7,5
【来源】
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
bool first=true;
int len=0, space=0, i;
string s;
getline(cin, s);
for (i=0; i<s.size(); i++){
if (s[i]==' ' || i==s.size()-1){
if (i == s.size()-1) len++;
if (space == 0){
if (first){
cout << len;
first = false;
}
else{
cout << "," << len;
}
len = 0;
}
space++;
}
else{
len++;
space = 0;
}
}
cout << endl;
return 0;
}