#include "stdio.h"
#include "malloc.h"
#include "string.h"
int maxsubstr(char *str1, char *str2, char **p)
{
char *p1, *p2, *p3;
p1 = str1;
char *substr;//子串
int max, len = max = 0;
while(*p1 != '\0'){
len = 0;
p2 = str2;
p3 = p1;
while(*p2 != '\0' && *p3 != '\0'){
if(*p2 == *p3){
len++;
p2++;
p3++;
if(len > max){
substr= p1;
max = len;
}
}
else p2++;
}
p1++;
}
*p = (char *)malloc((max + 1) * sizeof(char));//要比子串大一个存储空间,用字符串结束符
memcpy(*p, substr, max);
memset(*p + max , '\0', 1);//添加字符串结束符
return max;
}
int main()
{
char *s1="sdff";
char *s2="dddff";
char *p;//子串
// char *sub;
int i;//子串长度
printf("%s\n%s\n",s1,s2);
i = maxsubstr(s1,s2, &p);
printf("the max sub string is:%d\t%s\n",i, p);
return 0;
}
转载于:https://my.oschina.net/ervinfly/blog/309228