您的位置:首页 > 其它

统计单词(分割字符串,字符串函数的应用)

2017-01-14 14:18 281 查看
Problem Link:点击打开链接


题目描述

编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词) 
输入描述:
输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。


输出描述:
可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。


输入例子:
hello how are you.


输出例子:
5 3 3 3


AC code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<set>
#define LL long long
#define exp 1e-9
#define MAXN 1000010

using namespace std;

int main()
{
// freopen("D:\\in.txt","r",stdin);
char str[1010];
char *pStr;
int cnt;
while(gets(str))
{
pStr=strtok(str," .");
cnt=0;
while(pStr)
{
cnt++;
if(cnt==1)
printf("%d",strlen(pStr));
else
printf(" %d",strlen(pStr));
pStr=strtok(NULL," .");
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: