Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:11:03+00:00 2026-05-30T18:11:03+00:00

I have two strings the ending substring of first is the starting substring of

  • 0

I have two strings the ending substring of first is the starting substring of the second,ex

string left : ONESTRING
string right : STRINGTWO

I have to merge them so that the resulting string is

result string : ONESTRINGTWO

The length of the common substring is not known in advance.
If the starting and ending strings are not common, i need to return the concatenation of the strings.

This is what i am doing currently.

for(int i = 1;i< left.length();i++) {
        //substring of length "i" from last of left string
        string temp = left.substr(left.length() -1 -i,i);
        if(temp.length() < right.length()) {
            //check if the right string starts with the above substring 
            if (strncmp(right.c_str(), temp.c_str(), strlen(temp.c_str())) == 0 ) {
                // common substring found, save this result 
                found =  true;
                result = left.substr(0,left.length()-i-1) + right;
            }

        }
    }

if(found == true) {
    return result;
} else {
    return left + right;
}

I would be thankful for any pointers to a simpler implementation (in any language ).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:11:05+00:00Added an answer on May 30, 2026 at 6:11 pm

    By smart use of pointer arithmetic, you can skip the calls to substr (which do allocation) and strlen (which take O(n) time in the length of the string).

    std::string concat(std::string const &left, std::string const &right)
    {
        size_t n = left.length();
        for (size_t i=0; i<n; i++)
            if (std::strncmp(left.c_str() + i, right.c_str(), n - i) == 0)
                return left + (right.c_str() + n - i);
    
        return left + right;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two strings: the first's value is catdog and the second's is got.
I have two strings. How can I determine whether the first string is composed
I have two strings and would like to display the difference between them. For
Say I have two strings, String s1 = AbBaCca; String s2 = bac; I
I have two strings and I would like to check whether the first is
I have two strings. stringV1 = 123456; stringV2 = 102300456; First I need to
I have two strings: string a = 00001; /* which is decimal 1 I've
I have two strings. string a=50; string b=60; Now I want to comapre the
I have two strings: string ThePhone = XXX-XXX-XXXX string SkypeBlock = -<span style='display:none;'>-</span>; I
I have two strings which I'd like to compare: String and String: . Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.