统计单词(分割字符串,字符串函数的应用)
2017-01-14 14:18
281 查看
Problem Link:点击打开链接
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词)
输入描述:
输出描述:
输入例子:
输出例子:
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;
}
题目描述
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词) 输入描述:
输入包括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;
}
相关文章推荐
- Python实现统计英文单词个数及字符串分割代码
- 编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割
- 统计英文单词的个数的python代码 及 字符串分割
- 应用:把字符串的首字母大写并统计单词个数
- Python实现统计英文单词个数及字符串分割代码
- C# 实现统计字符串中不同单词的出现次数【百度笔试题】
- 题解:统计字符串中单词数目
- 统计一串字符串中,单词的个数,并输出这些单词,测试例子
- 运算符重载简单应用--统计输入同样字符串的次数
- 利用Map实现的统计每个输入字符串或则单词出现的次数
- 统计一串字符串中出现次数最多和次多的单词
- 字符串分割处理,strstr函数应用
- 统计字符串中单词个数
- 统计一个字符串中各个英文单词的出现的频数
- JS使用split分割字符串并应用正则匹配
- HDOJ-1251 统计难题 字典树的应用-----统计一组字符串中某前缀出现次数(字典树第一类应用),
- 题解:统计字符串中单词数目
- 统计字符串中单词个数的算法优化
- 用函数统计字符串的单词个数 空格数 字母数
- sql分割字符串并分组统计数量