CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Take 2 Word as a input and mixed it using JAVA

JAVA November 19, 2025 4 views 0 likes
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

Answer:

KboatStringMerge - Java Code

KboatStringMerge - Java Code


import java.util.Scanner;

public class KboatStringMerge
{
    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");
        }
    }
}

OUTPUT:

String Merge

Merge Two Strings (Alternate Characters)





4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285