#include <stdio.h>
#include <string.h>
void func(char *pstr, char *sstr);
int count_func(char *p, char *s);
int main()
{
char *data0 = "aaabbbbcccccccc'";
char data1[6];
func(data1,data0);
printf("%s\n",data1);
return 0;
}
void func(char *pstr, char *sstr)
{
int len = (int)strlen(sstr);
int count;
char tem[10];
int num0 = 0;
int num1 = 0;
char tempory[10];
int cur, i, p;
for(count = 1; count<=len/2; count++)//the length of string
{
for(cur = 0; cur+count<=len; cur++)
{
/***************************************************************************/
for(i=0; i<count; i++)//把要對比的字符串保存到tempory[]中
{
tempory[i] = sstr[cur+i];
}
tempory[i] = '\0';
num0 = count_func(sstr,tempory);//統計在字符串中字符串的個數
if(num0>num1)//如果大於原先的,則改變
{
num1 = num0;
strcpy(tem,tempory);
for(p=0;tem[p]!='\0';p++)
{
pstr[p] = tem[p];
}
pstr[p] ='\0';
}
}
}
}
int count_func(char *p, char *s)
{
int i, j;
int num=0;
for(i=0; p[i]!='\0'; i++)
{
for(j=0;s[j]!='\0';j++)
{
if(p[i+j]!=s[j])
break;
}
if(s[j]=='\0')
num++;
}
return num;
}
求一個字符串中出現次數最多的子串
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.