Given the strings s1 and s2 that are of the same length crea
Given the strings s1 and s2 that are of the same length, create a new string consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on (in other words the new string should consist of alternating characters of the reverse of s1 and s2). For example, if s1 contained hello\" and s2 contained \"world\", then the new string should contain \"odlllreohw\". Assign the new string to the variable s3.
Solution
import java.util.Arrays;
public class Demo{
public static void main(String []args){
String s1=\"hello\";// Take two strings of same length s1 and s2
String s2=\"world\";
int len=s1.length();// store length of any of the 2 strings in a variable as we have taken both strings of same length..
int k=0;
char a[]=new char[len+len];// create a temporary array in which we will store our output string .
for(int i=len-1,j=len-1;i>=0&&j>=0&&k<=len+len;i--,j--){
char temp1=s1.charAt(i);// take characters from reverse string s1
a[k]=temp1;// store character in a temporary array
k++;
char temp2=s2.charAt(i);//take characters from reverse string s2
a[k]=temp2;// store character in a temporary array
k++;
}
String s3 = Arrays.toString(a);// convert that array to string and assign it to variable s3
System.out.println(s3);
}
}
