EXE calculates the maximum public string of two strings

Source: Internet
Author: User

# 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.