w3resource

Java: Make a new string from two given string in such a way that, each character of two string will come respectively


83. Alternate Chars from Two Strings

Write a Java program to create a string from two given strings. This is so that each character of the two strings appears individually in the created string.

Visual Presentation:

Java String Exercises: Make a new string from two given string in such a way that, each character of two string will come respectively


Sample Solution:

Java Code:

import java.util.*;

// Define a class named Main
public class Main {

  // Method to mix two strings 'stng1' and 'stng2' character-wise
  public String stringMixing(String stng1, String stng2) {
    int len1 = stng1.length(); // Get the length of the first string 'stng1'
    int len2 = stng2.length(); // Get the length of the second string 'stng2'
    int max_len = Math.max(len1, len2); // Find the maximum length between two strings
    String newstring = ""; // Initialize an empty string to store the mixed string

    // Loop through up to the maximum length of the two strings
    for (int i = 0; i < max_len; i++) {
      // Append the character at index 'i' in 'stng1' if it exists
      if (i <= len1 - 1)
        newstring += stng1.substring(i, i + 1);
      // Append the character at index 'i' in 'stng2' if it exists
      if (i <= len2 - 1)
        newstring += stng2.substring(i, i + 1);
    }
    return newstring; // Return the mixed string
  }

  // Main method to execute the program
  public static void main(String[] args) {
    Main m = new Main(); // Create an instance of the Main class

    String str1 = "welcome"; // First string
    String str2 = "w3resource"; // Second string

    // Display the given strings and the new mixed string
    System.out.println("The given strings are: " + str1 + " and " + str2);
    System.out.println("The new string is: " + m.stringMixing(str1, str2));
  }
}

Sample Output:

The given strings  are: welcome  and  w3resource
The new string is: wwe3lrceosmoeurce

Flowchart:

Flowchart: Java String Exercises - Make a new string from two given string in such a way that, each character of two string will come respectively



For more Practice: Solve these Related Problems:

  • Write a Java program to interlace two strings character by character to form a new combined string.
  • Write a Java program to merge two strings by alternating characters from each string until one is exhausted.
  • Write a Java program to create a string by weaving together characters from two input strings and appending any remainder.
  • Write a Java program to interleave two strings such that every character from both appears in sequence in the output.

Go to:


PREV : Duplicate Each Character.
NEXT : Repeated Truncating Prefixes.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.