Home
/
Programs
/
Define a class to accept two strings of same length and form a new word in such a way that, the first character of the first word is followed by the first character of the second word and so on.Example :Input string 1 – BALLInput string 2 – WORD
OUTPUT : BWAOLRLD
Programming Example
Define a class to accept two strings of same length and form a new word in such a way that, the first character of the first word is followed by the first character of the second word and so on.
Example :
Input string 1 – BALL
Input string 2 – WORD
OUTPUT : BWAOLRLD
Study this program carefully to understand the logic, output, and explanation in a structured way.
import java.util.Scanner;
public class RansariStringMerge
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter String 1: ");
String s1 = in.nextLine();
System.out.println("Enter String 2: ");
String s2 = in.nextLine();
String str = "";
int len = s1.length();
if(s2.length() == len)
{
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same length");
}
}
}