Create a new string by alternately combining the characters of two halves of the string in reverse
Last Updated :
08 Mar, 2023
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.
Algorithm:
- Define a method named "solve" of void return type which takes string "s" as an input parameter.
- Create three int variables named "l", "x", and "y" and initialize them with a length of the string, half the length of the string, and the length of the string respectively.
- Define an empty string variable "p" to store the final output string.
- Start a while loop, and iterate until variable "x" is greater than 0 and variable "y" is greater than l/2.
- get the character at index (x-1) and add it to string "p". Then, decrement the value of "x".
- Again, get the character at index (y-1) and add it to string "p". Then, decrement the value of "y".
- If "y" is greater than l/2, then get the character at index (y-1) and add it to string "p".
- Then, decrement the value of "y".
- Finally, print the string "p".
Implementation:
C++
// 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;
}
Java
// 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(y - 1);
p += ch;
y--;
}
System.out.println(p);
}
// Driver method
public static void main(String args[])
{
String s = "sunshine";
// Calling function
solve(s);
}
}
Python3
# 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
C#
// 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.
PHP
<?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
?>
JavaScript
<script>
// JavaScript program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
// Function performing calculations
function solve( s) {
var l = s.length;
var x = l / 2;
var y = l;
// Calculating the two halves of
// string s as first and second
// The final string p
var p = "";
while (x > 0 && y > l / 2) {
// It joins the characters to
// final string in reverse order
var 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) {
var ch = s.charAt(x - 1);
p += ch;
y--;
}
document.write(p);
}
// Driver method
var s = "sunshine";
// Calling function
solve(s);
// This code is contributed by todaysgaurav
</script>
Time complexity: O(n) where n is the length of the string
Auxiliary Space: O(n)
Similar Reads
Reverse the substrings of the given String according to the given Array of indices Given a string S and an array of indices A[], the task is to reverse the substrings of the given string according to the given Array of indices.Note: A[i] ? length(S), for all i.Examples: Input: S = "abcdef", A[] = {2, 5} Output: baedcf Explanation: Input: S = "abcdefghij", A[] = {2, 5} Output: baed
10 min read
Transform the given String into two same Strings by removing at most one character Given a string S of length N where (N ? 1). The task is to check whether by removing at most one character from the given string it can be divided into two same sub-strings or not return YES or NO for possibilities. Examples: Input: N = 5, S = abzabOutput: YESExplanation: Character 'z' at index 3 ca
9 min read
Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions Given two integers A and B, the task is to generate and print a string str such that: str must only contain the characters 'a' and 'b'.str has length A + B and the occurrence of the character 'a' is equal to A and the occurrence of character 'b' is equal to BThe sub-strings "aaa" or "bbb" must not o
6 min read
Modify string by increasing each character by its distance from the end of the word Given a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word) Examples: Input: S = "acm fkz" Output: "cdm hlz" Explanation: There are 2 words in the given string {"acm", "fkz"} For "acm": a
7 min read
Minimal moves to form a string by adding characters or appending string itself Given a string S, we need to write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step, we can: Add any character at the end of the string.or, append the string to the string itself.The above steps can be ap
8 min read