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

  • برمجة سي بلس بلس
  • برمجة

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

الأجوبة

#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;    
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...