给定字符串A和B,输出A和B中的最大公共子串。 比如A="aocdfe" B="pmcdfa" 则输出"cdf"
void commonstring(char *str1, char *str2, char *maxSubstr)
{
int i = 0, j = 0, k = 1, maxPos = -1, maxLen = 0;
if ((NULL == str1) || (NULL == str2) || (NULL == maxSubstr))
{
return NULL;
}
for (i=0; i < strlen(str1); i++)
{
for (j=0; j < strlen(str2); j++)
{
if (str1[i] == str2[j])
{
for (k=1; (str1[i+k] == str2[j+k]) && (str1[i+k]!='\0'); k++)
;
}
if (k > maxLen)
{
maxPos = i;
maxLen = k;
}
}
}
if (-1 == maxPos)
{
maxSubstr[0] = '\0';
}
else
{
memcpy(maxSubstr, str1+maxPos, maxLen);
maxSubstr[maxLen] = '\0';
}
}