统计若干行英文中单词的个数和每个单词出现的次数。
[输入]若干行英语。
[输出]单词数量及每个单词出现的次数
#include<stdio.h>
#include<string.h>
int main()
{
char str[500],temp[10];
char word[50][10],count[50]={0};
int i=0,j=0,k,t;
gets(str);
while(str[i]!='\0')
{
if(i==0 && str[0]!=' ')
{
sscanf(str,"%s",temp);
strcpy(word[j],temp);
count[j++]=1;
}
else if(str[i-1]==' ' && str[i]!=' ')
{
sscanf(str+i,"%s",temp);
for(k=0;k<j;k++)
if(strcmp(word[k],temp)==0)
{
count[k]++;
break;
}
if(k==j)
{
strcpy(word[j],temp);
count[j++]=1;
}
}
i++;
}
for(i=0;i<j-1;i++)
for(k=i+1;k<j;k++)
if(count[i]<count[k])
{
strcpy(temp,word[i]);
strcpy(word[i],word[k]);
strcpy(word[k],temp);
t = count[i];
count[i] = count[k];
count[k] = t;
}
t =0;
for(i=0;i<j;i++)
{
printf("%s:%d\n",word[i],count[i]);
t +=count[i];
}
printf("总个数:%d\n",t);
return 0;
}