Join Alternate Characters From Two Strings

Write a program to input two strings in uppercase from the user.

Now form and display a new string by taking each character from the first string from left to right and of the second string from right to left.

The letters should be taken alternatively from each string.

Also note that the lengths of both the strings should be the same.

Example:
INPUT:
First string: HISTORY
Second string: SCIENCE
OUTPUT:
HEICSNTEOIRCYS

import java.io.*;
class Alternate{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("First string: ");
        String s1 = br.readLine().toUpperCase();
        System.out.print("Second string: ");
        String s2 = br.readLine().toUpperCase();
        if(s1.length() != s2.length()){
            System.out.println("Unequal lengths!");
            return;
        }
        String s3 = "";
        int p1 = 0;
        int p2 = s2.length() - 1;
        while(true){
            if(p1 == s1.length() || p2 < 0)
                break;
            s3 += s1.charAt(p1);
            s3 += s2.charAt(p2);
            p1++;
            p2--;
        }
        System.out.println(s3);
    }
}

Write a program in Java to accept two strings from the user.

Now create a new string which is created by the combination of the above two strings, taking one character alternately from those two strings, followed by all the remaining characters of the longer string if any.

Sample Input:
First String: WALNUT
Second String:TOP
Sample Output:
Resultant String: WTAOLPNUT

import java.io.*;
class Alternate{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("First string: ");
        String s1 = br.readLine();
        System.out.print("Second string: ");
        String s2 = br.readLine();
        s1 = s1.toUpperCase();
        s2 = s2.toUpperCase();
        String s3 = "";
        int i = 0;
        while(i < s1.length() && i < s2.length()){
            s3 += s1.charAt(i) + "" + s2.charAt(i);
            i++;
        }
        if(i < s1.length())
            s3 += s1.substring(i);
        if(i < s2.length())
            s3 += s2.substring(i);
        System.out.println(s3);
    }
}

7 thoughts on “Join Alternate Characters From Two Strings

        • The 2nd alternate String program is this:
          import java.io.*;
          class Alter{
          public static void main(String args[])throws IOException{
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print(“First string: “);
          String a = br.readLine();
          System.out.print(“Second string: “);
          String b = br.readLine();
          String s3 = “”;
          int l1=a.length();
          int l2=b.length();
          String c,d;
          if(l1>l2){
          c=a;
          d=b;
          }
          else if (l2>l1) {
          c=b;
          d=a;

          }
          else
          {
          System.out.println(“EQUAL STRINGS”);
          return;
          }
          for (int i=0;i<d.length() ;i++ ) {
          s3+=a.charAt(i)+""+b.charAt(i);

          }
          s3+=c.substring(d.length());
          System.out.println("NEW STRING:"+s3);
          }
          }

        • import java.io.*;
          class Alter{
          public static void main(String args[])throws IOException{
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print(“First string: “);
          String a = br.readLine();
          System.out.print(“Second string: “);
          String b = br.readLine();
          String s3 = “”;
          int l1=a.length();
          int l2=b.length();
          String c,d;
          if(l1>l2){
          c=a;
          d=b;
          }
          else if (l2>l1) {
          c=b;
          d=a;

          }
          else
          {
          System.out.println(“EQUAL STRINGS”);
          return;
          }
          for (int i=0;i<d.length() ;i++ ) {
          s3+=a.charAt(i)+""+b.charAt(i);

          }
          s3+=c.substring(d.length());
          System.out.println("NEW STRING:"+s3);
          }
          }

  1. Sir,a program is given which states that:
    WAP in Java to accept n numbers in a single dimensional array. Now arrange the numbers in ascending order using insertion sort technique .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.