源代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *maxsubstr(char shortstring[], char longstring[])
{
int i;
int j;
char *substr = malloc(256);
longstring[strlen(longstring) - 1] = 'M';
if(strstr(longstring, shortstring) != NULL)
return shortstring;
for(i = strlen(shortstring) - 1; i > 0; i--)
for(j = 0; j < strlen(shortstring) - i; j++)
{
memcpy(substr, &shortstring[j], i);
substr[i] = '/0';
if(strstr(longstring, substr) != NULL)
return substr;
}
return NULL;
}
int main(int argc, char **argv)
{
char *str1 = malloc(256);
char *str2 = malloc(256);
char *common = NULL;
puts("please input two strings:");
scanf("%s %s", str1, str2);
if(strlen(str1) < strlen(str2))
common = maxsubstr(str1, str2);
else
common = maxsubstr(str2, str1);
printf("the max common string is: %s/n", common);
printf("/n%s", str2);
return 0;
}