https://pta.patest.cn/pta/test/14/exam/4/question/806
5-26 单词长度 (15分)
你的程序要读入一行文本,其中以空格分隔为若干个单词,以.
结束。你要输出每个单词的长度。这里的单词与语言无关,可以包括各种符号,比如it's
算一个单词,长度为4。注意,行中可能出现连续的空格;最后的.
不计算在内。
输入格式:
输入在一行中给出一行文本,以.
结束
提示:用scanf("%c",...);
来读入一个字符,直到读到.
为止。
输出格式:
在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。
输入样例:
It's great to see you here.
输出样例:
4 5 2 3 3 4
注意最后一个单词的句号之前可能有空格
这种情况:
It's great to see you here .
代码:
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #define INF 100000000 using namespace std; string s; vector<int> ans; int check(string s) { for(int i=0;i<s.size();i++) { if(s[i]=='.') { return i; } } return s.size(); } int main() { while(cin>>s) { int tmp=check(s); ans.push_back(tmp); if(tmp!=s.size()) break; } if(ans[ans.size()-1]==0) { vector<int>::iterator i=ans.end()-1; ans.erase(i); } for(int i=0;i<ans.size();i++) { if(i==0) printf("%d",ans[i]); else printf(" %d",ans[i]); } return 0; }