Q:

Write a C++ program to concat two given strings. If the given strings have different length remove the characters from the longer string

0

Write a C++ program to concat two given strings. If the given strings have different length remove the characters from the longer string

Sample Output:

abcbcd
PythonPython
JSon

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

 string test(string s1, string s2)
        {
            if (s1.length() < s2.length())
            {
                return s1 + s2.substr(s2.length() - s1.length());
            }
            else if (s1.length() > s2.length())
            {
                return s1.substr(s1.length() - s2.length()) + s2;
            }
            else
            {
                return s1 + s2;
            }
        }
        
int main() 
 {
  cout << test("abc", "abcd") << endl;  
  cout << test("Python", "Python") << endl; 
  cout << test("JS", "Python") << endl;  
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now