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

#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);
	
}

  

posted on 2012-10-23 23:31  为梦飞翔  阅读(3393)  评论(0编辑  收藏  举报

导航