Merge String Characters Program in Java
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. [10]
Example:
Input string 1-BALL
Input String 2- WORD
OUTPUT: BWAOLRLD
import java.util.Scanner;
public class MergeString
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String st1,st2;
String newSt = "";
System.out.print("Enter a string1:");
st1 = sc.nextLine();
System.out.print("Enter a string2:");
st2 = sc.nextLine();
int l = st1.length();
for (int i = 0; i < l; i++)
{
newSt = newSt + st1.charAt(i) + st2.charAt(i);
}
System.out.println(newSt);
}
}
Output:
Enter a string1:BALL Enter a string2:WORD BWAOLRLD