标题
单词统计
描述
输入字符串string[N](N≤100),各个单词以空格隔开,单词长度小于等于8,输入单词word[M](M≤8),在string中查找出相同的单词并统计该单词出现的次数信息,输出单词和出现次数的信息, 数据之间空一格空格。主函数输入字符串和待统计单词,编写函数count()实现统计和信息输出。
时间限制
1
内存限制
10000
类别
1
输入说明
输入一行字符以空格隔开各个单词,输入要统计的单词。
输出说明
格式输出:输出单词及其出现的次数信息,数据之间空一格。
输入样例
dog cat dog dog the abc dog hahe
dog
输出样例
dog 4
提示
采用重循环结构实现计算过程,输出数据之间空一格。
先将输入串分解并统计出单词的数量,然后检索输入的字符串就可以了
或者就是直接在输入串中逐个匹配
#include <stdio.h>
#include <string.h>
int main()
{
int ns,nw,i,j,k,count=0;
char s[1000],w[100];
gets(s);
gets(w);
ns = strlen(s);
nw = strlen(w);
for(i=0;i<s-w;i++)
{
j = 0;
k = i;
while(j<nw)
{
if(s[k] != w[j])
break;
k++;
j++;
}
if(j==nw)
{
count++;
i=k-1;
}
}
printf("%s %d",w,count);
return 0;
}