您的位置:首页 > 其它

求一个字符串中出现次数最多的子串

2015-06-08 15:53 148 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: