Program to mixup characters of 2 strings in JAVA

In this article we’ll see a program to mixup characters of 2 string in JAVA.

Input

Consider a two strings as Good and Day.

Code

public class StringMixup
{
  public static void main(String[] args)
  {
    String str1 = "Good", str2 = "Day";
    int i = 0, str1Length = str1.length(), str2Length = str2.length();
    String output = "";

    while(i < str1Length || i < str2Length)
    {
      output += i < str1Length ? str1.charAt(i) : "";
      output += i < str2Length ? str2.charAt(i) : "";
      i++;
    }

    System.out.println("String1: " + str1);
    System.out.println("String2: " + str2);
    System.out.println("Output: " + output);
  }
}

Output

String1: Good
String2: Day
Output: GDoaoyd

Happy 😄 coding

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳