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

👁 77 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

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");
        }
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.