Create a new string by alternately combining the characters of two halves of the string in reverse

Given a string s, create a new string such that it contains the characters of the two halves of the string s combined alternately in reverse order.

Examples:

Input : s = carbohydrates
Output : hsoebtraarcdy

Input : s = sunshine
Output : sennuish

Explanation:
Example 1: Two halves of the string carbohydrate are carboh and ydrates. As they needed to be added in reverse alternately, start with h from first half then s from second half followed by o from first half, e from second half and so on. The string p comes out to be hsoebtraarcdy. If one of the string is completely finished then simply add the remaining characters of the other string in reverse order.
Example 2: The two halves of the string are suns and hine. String sennuish is the desired string p.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for creating a string 
// by alternately combining the
// characters of two halves 
// in reverse 
#include <bits/stdc++.h>
using namespace std;
  
// Function performing calculations
void solve(string s)
{
    int l = s.length();
    int x = l / 2;
    int y = l;
      
    // Calculating the two halves
    // of string s as first and 
    // second. The final string p
    string p = "";
    while (x > 0 && y > l / 2) {
          
        // It joins the characters to
        // final string in reverse order
        p += s[x - 1];
        x--;
          
        // It joins the characters to
        // final string in reverse order
        p += s[y - 1];
        y--;
    }
      
    if (y > l / 2) {
        p += s[y - 1];
        y--;
    }
      
    cout << p;
}
  
// Driver code
int main()
{
    string s = "sunshine";
      
    // Calling function
    solve(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for creating a string 
// by alternately combining the
// characters of two halves 
// in reverse 
import java.io.*;
  
class GFG {
      
    // Function performing calculations
    public static void solve(String s)
    {
        int l = s.length();
        int x = l / 2;
        int y = l;
      
        // Calculating the two halves of 
        // string s as first and second
        // The final string p
        String p = "";
        while (x > 0 && y > l / 2) {
          
            // It joins the characters to
            // final string in reverse order
            char ch = s.charAt(x - 1);
            p += ch;
            x--;
              
            // It joins the characters to
            // final string in reverse order
            ch = s.charAt(y - 1);
            p += ch;
            y--;
        }
          
        if (y > l / 2) {
            char ch = s.charAt(x - 1);
            p += ch;
            y--;
        }
        System.out.println(p);
    }
      
    // Driver method
    public static void main(String args[])
    {
        String s = "sunshine";
          
        // Calling function
        solve(s);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program for creating a string 
# by alternately combining the
# characters of two halves 
# in reverse 
  
# Function performing calculations
def solve(s) :
    l = len(s)
    x = l // 2
    y = l
       
    # Calculating the two halves
    # of string s as first and 
    # second. The final string p
    p = ""
    while (x > 0 and y > l / 2) :
  
        # It joins the characters to
        # final string in reverse order
        p =  p + s[x - 1]
        x = x - 1
           
        # It joins the characters to
        # final string in reverse order
        p = p + s[y - 1]
        y = y - 1
      
       
    if (y > l // 2) :
        p = p + s[y - 1]
        y = y - 1
      
    print (p)
  
# Driver code
s = "sunshine"
  
# Calling function
solve(s)
  
  
# This code is contributed by Nikita Tiwari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for creating a string 
// by alternately combining the
// characters of two halves 
// in reverse 
using System;
  
class GFG {
      
    // Function performing calculations
    public static void solve(string s)
    {
        int l = s.Length;
        int x = l / 2;
        int y = l;
      
        // Calculating the two halves of 
        // string s as first and second
        // The final string p
        string p = "";
        while (x > 0 && y > l / 2) {
          
            // It joins the characters to
            // final string in reverse order
            char ch = s[x - 1];
            p += ch;
            x--;
              
            // It joins the characters to
            // final string in reverse order
            ch = s[y - 1];
            p += ch;
            y--;
        }
          
        if (y > l / 2)
        {
            char ch = s[x - 1];
            p += ch;
            y--;
        }
        Console.WriteLine(p);
    }
      
    // Driver method
    public static void Main()
    {
        string s = "sunshine";
          
        // Calling function
        solve(s);
    }
}
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// Python 3 program for creating a string 
// by alternately combining the
// characters of two halves in reverse 
  
// Function performing calculations
function solve($s)
{
    $l = strlen($s);
    $x = $l / 2;
    $y = $l;
      
    // Calculating the two halves
    // of string s as first and 
    // second. The final string p
    $p = "";
    while ($x > 0 && $y > $l / 2) 
    {
          
        // It joins the characters to
        // final string in reverse order
        $p = $p.$s[$x - 1];
        $x--;
          
        // It joins the characters to
        // final string in reverse order
        $p = $p.$s[$y - 1];
        $y--;
    }
      
    if ($y > $l / 2) 
    {
        $p = $p.$s[$y - 1];
        $y--;
    }
      
    echo $p;
}
  
// Driver code
$s = "sunshine";
      
// Calling function
solve($s);
  
// This code is contributed
// by ChitraNayal
?>

chevron_right



Output:

sennuish


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 : chitranayal, Akanksha_Rai



Article Tags :
Practice Tags :


1


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