Interchanging first and second halves of strings - GeeksforGeeks

06 Aug, 2021
Given two strings and . Create two new strings by exchanging the first half and second half of one of the strings with the first half and second half of the other string respectively.
Examples: 
Input : fighter
        warrior
Output :warhter
        figrior

Input :remuneration
       day
Output :dration
        remuneay
In the first example, fighter is made up of two parts fig and hter. Similarly, warrior is also made up of two parts war and rior. Interchange the first part of both the strings to create two new strings warhter and figrior. Similarly, in the second example first part of remuneration which is remune is exchanged with the first part of day to create dration and remuneay.
C++
Java
Python3
C#
PHP
Javascript
// CPP code to create two new strings// by swapping first and second halves#include<bits/stdc++.h>using namespace std; // Function to concatenate two// different halves of given stringsvoid swapTwoHalves(string a , string b){    int la = a.length();    int lb = b.length();         // 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/    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;} // Driver functionint main(){    string a = "remuneration";    string b = "day";    swapTwoHalves(a, b);       return 0;}
// Java code to create two new strings// by swapping first and second halvesimport java.io.*;class ns{        // Function to concatenate two    // different halves of given strings    public static void swapTwoHalves(String a,                                     String b)    {        int la = a.length();        int lb = b.length();                 // Creating new strings by        // exchanging the first half        // of a and b. Please refer below        // for details of substring.        // https://www.geeksforgeeks.org/java-lang-string-substring-java/        String c = a.substring(0,la/2) +                   b.substring(lb/2,lb);        String d = b.substring(0,lb/2) +                  a.substring(la/2,la);                     System.out.println(c + "\n" + d);    }    public static void main (String args[])    {        // Given strings        String a = "remuneration";        String b = "day";                 // Calling function        swapTwoHalves(a, b);    }}
# Python 3 code to create two new strings# by swapping first and second halves  # Function to concatenate two# different halves of given stringsdef swapTwoHalves( a ,b) :    la = len(a)    lb = len(b)          # 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/    c = a[0:la//2] + b[lb//2: lb]    d = b[0: lb//2] + a[la//2: la]              print( c, "\n" , d )  # Driver functiona = "remuneration"b = "day"swapTwoHalves(a, b)  # This code is contributed by Nikita Tiwari
// C# code to create two// new strings by swapping// first and second halvesusing System; class GFG{    // Function to concatenate    // two different halves    // of given strings    static void swapTwoHalves(string a ,                              string b)    {        int la = a.Length;        int lb = b.Length;                 // Creating new strings        // by exchanging the        // first half of a and b.        string c = a.Substring(0, la / 2) +                   b.Substring(lb / 2,                               lb / 2 + 1);        string d = b.Substring(0, lb / 2) +                   a.Substring(la / 2,                               la / 2);                     Console.Write (c + "\n" +                       d + "\n");    }             // Driver Code    static void Main()    {        string a = "remuneration";        string b = "day";        swapTwoHalves(a, b);    }} // This code is contributed by// Manish Shaw(manishshaw1)
<?php// PHP code to create two// new strings by swapping// first and second halves // Function to concatenate // two different halves of// given stringsfunction swapTwoHalves($a , $b){    $la = strlen($a);    $lb = strlen($b);         // Creating new strings    // by exchanging the first    // half of a and b.    $c = substr($a, 0, intval($la / 2)) .         substr($b, intval($lb / 2), $lb);    $d = substr($b, 0, intval($lb / 2)) .         substr($a, intval($la / 2), $la);             echo ($c . "\n" .          $d . "\n");} // Driver Code$a = "remuneration";$b = "day"; swapTwoHalves($a, $b); // This code is contributed by// Manish Shaw(manishshaw1)?>
<script> // JavaScript code to create two new strings// by swapping first and second halves// Function to concatenate two// different halves of given stringsfunction swapTwoHalves(a, b)    {        var la = a.length;        var lb = b.length;                 // Creating new strings by        // exchanging the first half        // of a and b. Please refer below        // for details of substring.        // https://www.geeksforgeeks.org/java-lang-string-substring-java/        var c = a.substring(0,la/2) +                   b.substring(lb/2,lb);        var d = b.substring(0,lb/2) +                  a.substring(la/2,la);                     document.write(c +"<br>" + "\n" + d);    }            // Given strings        var a = "remuneration";        var b = "day";                 // Calling function        swapTwoHalves(a, b);         // This code is contributed by shivanisinghss2110</script>
Output: 
remuneay
dration 
YouTubeGeeksforGeeks502K subscribersInterchanging first and second halves of stings | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:34•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Qo0zMKGEr18" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
manishshaw1
kk9826225
shivanisinghss2110
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Array of Strings in C++ (5 Different Ways to Create)
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters