#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *commanstring(char shortstring[],char longstring[])
{
int i,j;
char *substring = malloc(256);
if(strstr(longstring,shortstring)!=NULL)
return shortstring;
for(i=strlen(shortstring)-1;i>0;i--)
{
for(j=0;j<=strlen(shortstring)-i;j++)
{
memcpy(substring,&shortstring[j],i);
substring[i]='\0';
if(strstr(longstring,substring)!=NULL)
return substring;
}
}
return NULL;
}
void main(void)
{
char *str1 = "aocdfe";
char *str2 = "pmcdfa";
char *comman = NULL;
if(strlen(str1)>strlen(str2))
comman= commanstring(str2,str1);
else
comman = commanstring(str1,str2);
printf("the longest comman string is:%s\n",comman);
}
|