Interchanging first and second halves of stings

Given two strings a and b. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 strings
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 substr.
    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 function
int main()
{
    string a = "remuneration";
    string b = "day";
    swapTwoHalves(a, b);    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to create two new strings
// by swapping first and second halves
import 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.
        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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to create two new strings
# by swapping first and second halves
  
  
# Function to concatenate two 
# different halves of given strings
def 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.
    c = a[0:la//2] + b[lb//2: lb]
    d = b[0: lb//2] + a[la//2: la]
           
    print( c, "\n" , d )
   
# Driver function
a = "remuneration"
b = "day"
swapTwoHalves(a, b) 
  
  
# This code is contributed by Nikita Tiwari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create two 
// new strings by swapping
// first and second halves
using 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)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to create two 
// new strings by swapping
// first and second halves
  
// Function to concatenate  
// two different halves of 
// given strings
function 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)
?>

chevron_right



Output:

remuneay
dration


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : manishshaw1



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.