In: Computer Science
Given the strings s1 and s2 that are of the same length, create a new string s3 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
package lastsecondlast;
public class LastSecondLast {
public static void main(String[] args) {
String s1 = "Hello"; //string one
String s2 = "World"; //string two of same length
String s3 = ""; //resultant string
int temp = s1.length()-1; // calculating length of string
for(int i=0;i<s1.length();i++) //loop to get resultant string
{
s3 = s3 + s1.charAt(temp); //adding alternatively alphabets of both string
s3 = s3 + s2.charAt(temp);
temp--; //decrementing variable to go to second last element and so on
}
System.out.println(s3); //printing resultant string
}
}
Output:-
Since no language was mentioned I did it in Java.
If you want to take user input, you can use Scanner object.
In this example, I have used Hello and World as two strings.
Comment down if you want this program in C++ or Python.
Note:- Please comment down if you face any problem. :)