Sunday, July 9, 2023

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.

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

Solution:
import java.util.Scanner;

public class Year2022Q3
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String st1,st2;
        String newStr = "";
        System.out.println("Enter 2 string of same length: ");
        System.out.print("Enter first string:");
        st1 = sc.nextLine();
        System.out.print("Enter second string:");
        st2 = sc.nextLine();
        int len = st1.length();
        for (int i = 0; i < len; i++)
        {
            newStr = newStr + st1.charAt(i) + st2.charAt(i);
        }
        System.out.println(newStr);
    }
}

Output:

No comments:

Post a Comment

Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...