//统计一串字符串中出现次数最多和次多的单词
#include<stdio.h>
#include<string.h>
#include<iostream>
int compare(const char * st1,const char * st2)
{
if (strcmp(st1,st2)==0)
return 1;
else
return 0;
}
void PickWord(const char* pInPut,char *pOutPut1,char *pOutPut2)
{
char tem[BUFSIZ][40]={'\0'};
const char *p=pInPut;
int j=0;
int i=0;
int l=0;
char *po1=pOutPut1;
char *po2=pOutPut2;
//用二维数组存储单词
while(*p!='\0')
{
if(((*p>='a')&&(*p<='z'))||((*p>='A')&&(*p<='Z'))||(*p=='\''))
{
tem[j][i++]=*p ;
p++;
}
else
{
tem[j][i]='\0';
j++;
p++;
i=0;}
}
int m,n,max1,max2;
int max[BUFSIZ]={0};
//统计每个单词的次数
for (n=0;n<=j;n++)
{
max[n]=0;
for(m=0;m<=j;m++)
{
max[n]+=compare(tem[n],tem[m]);
}
}
//找出最多的单词
max1=0;
max2=0;
for (n=0;n<=j;n++)
{
if (max[n]>max1)
{
max1=max[n];
i=n;
}
}
for (l=0;l<strlen(tem[i]);l++)
{
*pOutPut1++=tem[i][l];
}
*pOutPut1='\0';
//将最多的单词的统计个数置0
for (n=0;n<=j;n++)
{
if ((max[n]==max1)&&compare(tem[n],po1))
max[n]=0;
}
//找出次多的单词
for (n=1;n<=j;n++)
{
if (max[n]>max2)
{
max2=max[n];
i=n;
}
}
for (l=0;l<strlen(tem[i]);l++)
{
*pOutPut2++=tem[i][l];
}
*pOutPut2='\0';
}
void main()
{
const char a[]="I'm so good,and good and good no no no no";
char* b=(char *)malloc(sizeof(char)*40);
char* c=(char *)malloc(sizeof(char)*40);
PickWord(a,b,c);
std::cout<<b<<std::endl;
std::cout<<c<<std::endl;
free(b);
free(c);
}
#include<stdio.h>
#include<string.h>
#include<iostream>
int compare(const char * st1,const char * st2)
{
if (strcmp(st1,st2)==0)
return 1;
else
return 0;
}
void PickWord(const char* pInPut,char *pOutPut1,char *pOutPut2)
{
char tem[BUFSIZ][40]={'\0'};
const char *p=pInPut;
int j=0;
int i=0;
int l=0;
char *po1=pOutPut1;
char *po2=pOutPut2;
//用二维数组存储单词
while(*p!='\0')
{
if(((*p>='a')&&(*p<='z'))||((*p>='A')&&(*p<='Z'))||(*p=='\''))
{
tem[j][i++]=*p ;
p++;
}
else
{
tem[j][i]='\0';
j++;
p++;
i=0;}
}
int m,n,max1,max2;
int max[BUFSIZ]={0};
//统计每个单词的次数
for (n=0;n<=j;n++)
{
max[n]=0;
for(m=0;m<=j;m++)
{
max[n]+=compare(tem[n],tem[m]);
}
}
//找出最多的单词
max1=0;
max2=0;
for (n=0;n<=j;n++)
{
if (max[n]>max1)
{
max1=max[n];
i=n;
}
}
for (l=0;l<strlen(tem[i]);l++)
{
*pOutPut1++=tem[i][l];
}
*pOutPut1='\0';
//将最多的单词的统计个数置0
for (n=0;n<=j;n++)
{
if ((max[n]==max1)&&compare(tem[n],po1))
max[n]=0;
}
//找出次多的单词
for (n=1;n<=j;n++)
{
if (max[n]>max2)
{
max2=max[n];
i=n;
}
}
for (l=0;l<strlen(tem[i]);l++)
{
*pOutPut2++=tem[i][l];
}
*pOutPut2='\0';
}
void main()
{
const char a[]="I'm so good,and good and good no no no no";
char* b=(char *)malloc(sizeof(char)*40);
char* c=(char *)malloc(sizeof(char)*40);
PickWord(a,b,c);
std::cout<<b<<std::endl;
std::cout<<c<<std::endl;
free(b);
free(c);
}