KnowledgeBoat Logo

Computer Applications

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

Java

Java String Handling

ICSE 2022

17 Likes

Answer

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

Variable Description Table

Program Explanation

Output

BlueJ output of 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 : BWAOLRLDBlueJ output of 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

Answered By

5 Likes


Related Questions