# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <iostream. h>
Char * commonstring (char * str1, char * str2)
{
Char * struct STR, * longstr;
Char * substr;
If (str1 = NULL | str2 = NULL)
Return NULL;
// Reverse STR and longstr point to short and long strings respectively.
Strlen (str1)> = strlen (str2 )? (Longstr = str1, strong STR = str2) :( strong STR = str1, longstr = str2 );
// Use the strstr () function to determine whether the short string is in the long string.
If (strstr (longstr, reverse Str )! = NULL)
Return str2;
// Apply for an empty string with a short String Length plus 1
Substr = (char *) malloc (sizeof (char) * (strlen (character Str) + 1 ));
For (INT I = strlen (reverse Str)-1; I> 0; I --)
{
For (Int J = 0; j <= (INT) strlen (reverse Str)-I; j ++)
{
Memcpy (substr, & Other STR [J], I); // copy part of the short string to substr
Substr [I] = '/0'; // Its length is gradually reduced
If (strstr (longstr, substr )! = NULL) // If substr can be found in a long string, return substr;
Return substr;
}
}
Return NULL;
}
Int main ()
{
Char str1 [100];
Char str2 [100];
Cout <"enter a long string:" <Endl;
Gets (str1 );
Cout <"enter a short string:" <Endl;
Gets (str2 );
Char * commonstr = commonstring (str1, str2 );
Cout <commonstr <Endl;
Return 0;
}