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


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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM