您的位置:首页 > 其它

统计一个字符串中各个英文单词的出现的频数

2012-10-23 23:31 405 查看
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char destr[]="   hello hello   sfs  che heloo  dsljd hello hello    ";
int num=1;
char *p=destr;
while(*p!='\0')
{
if(*p>='A'&&*p<='Z')
{
*p=*p-'A'+'a';
}
if(*p==' ')
{
num++;
*p='\0';
}
p++;
}
int *array=(int*)malloc(num*sizeof(int));
array[0]=1;
for(int i=1;i<num;i++)
array[i]=0;
char *tempA=destr;
for(i=0;i<num-1;i++)
{
tempA+=(strlen(tempA)+1);
char *tempB=destr;
for(int j=0;j<num;j++)
{
if(strcmp(tempA,tempB)==0)
{
array[j]++;
break;
}
else
tempB+=(strlen(tempB)+1);
}

}
tempA=destr;
for(i=0;i<num;i++)
{
if(array[i]&&*tempA!='\0') cout<<tempA<<" 出现的次数:"<<array[i]<<endl;
tempA+=(strlen(tempA)+1);
}
free(array);

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐