Interchanging first and second halves of strings | CPP code to create two new strings by swapping first and second halves ; Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . Please refer below for details of substr . https : www . geeksforgeeks . org / stdsubstr - in - ccpp / ; Driver function ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swapTwoHalves ( string a , string b ) { int la = a . length ( ) ; int lb = b . length ( ) ; string c = a . substr ( 0 , la / 2 ) + b . substr ( lb / 2 , lb ) ; string d = b . substr ( 0 , lb / 2 ) + a . substr ( la / 2 , la ) ; cout << c << endl << d << endl ; } int main ( ) { string a = " remuneration " ; string b = " day " ; swapTwoHalves ( a , b ) ; return 0 ; }