题目:编程实现查找两个字符串的最大公共子串
示例:”aocdfe”和”pmcdfa”最大公共子串为”cfd”
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = {0};
char str2[20] = {0};
printf("Please input two strings:\n");
scanf("%s%s",str1,str2);
char *p1 = str1;
char *p2 = str2;
int max = 0, num = 0;
char *start;
while(*p1 != '\0')
{
p2 = str2;
while(*p2 != '\0')
{
char *begin = p1;
char *begin2 = p2;
num = 0;
while(*begin != '\0' && *begin2 != '\0')
{
if(*begin == *begin2)
{
num++;
begin++;
begin2++;
}
else break;
}
if(num > max)
{
max = num;
start = p1;
}
p2++;
}
p1++;
}
while(max--)
printf("%c",*start++);
printf("\n");
return 0;