統計一個字符串中各個英文單詞的出現的頻數


#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