通过交替组合字符的两半的字符串来创建一个新字符串
给定一个字符串s,创建一个新字符串,它包含字符串s 的两半的字符,它们以相反的顺序交替组合。
例子:
Input : s = carbohydrates
Output : hsoebtraarcdy
Input : s = sunshine
Output : sennuish
解释:
示例 1:字符串碳水化合物的两半是carboh和ydrates 。由于它们需要交替反向添加,所以从前半部分的h开始,然后从后半部分的 s开始,然后从前半部分的o开始,从后半部分的e开始,依此类推。字符串p是hsoebtraarcdy 。如果其中一个字符串完全完成,则只需以相反的顺序添加另一个字符串的剩余字符。
示例 2:字符串的两半是suns和hine 。字符串sennuish是所需的字符串p 。
C++
// C++ program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
#include
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(x - 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
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
输出:
sennuish