统计字符串中单词总个数以及每个单词出现的个数 ,代码没问题 却无法运行
#include<stdio.h>#include<string.h>
void main()
{
char *str="Tom is Tom and you are you";//
char words[100][20]={0};
int total=0;//统计总个数
int wordcount[100]={0};//统计单词频率
char *p=str;
char temp[20]={0};
int tempcount=0;
int i=0;
while(p)
{
if(*p==' ')
{
for(i=0;i<total;++i)
{
if(strcmp(words[i],temp)==0)
{
wordcount[i]++;
}
}
if(i==total)
{
strcpy(words[total++],temp);
strcpy(temp,"");
tempcount=0;
}
}
else
{
temp[tempcount++]=*p;
}
p++;
}
printf("单词总数:%d\n",total);
for(i=0;i<total;++i)
{
printf("单词:%s 数量:%d\n",words[i],wordcount[i]);
}
}