Alternatively Merge two Strings in Java

Given 2 strings, merge them in an alternate way, i.e. the final string’s first character is the first character of the first string, the second character of the final string is the first character of the second string and so on. And if once you reach end of one string while if another string is still remaining then append the remaining of that string to final string

Examples:

Input : string 1 :"geeks"
        string 2 :"forgeeks"
Output: "gfeoerkgseeks"
Explanation : The answer contains characters from alternate strings, and once 
the first string ends the remaining of the second string is added to the final string

Input :string 1 :"hello"
        string 2 :"geeks"
Output : hgeelelkos

The idea is simple, we create a result string. We one by one append characters of both given strings in alternate style.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to alternatively merge two strings
#include <bits/stdc++.h>
using namespace std;
  
// Function for alternatively merging two strings 
string merge(string s1, string s2) 
    // To store the final string 
    string result = "";
  
    // For every index in the strings 
    for (int i = 0; i < s1.length() ||
                    i < s2.length(); i++) 
    
  
        // First choose the ith character of the 
        // first string if it exists 
        if (i < s1.length()) 
            result += s1[i];
  
        // Then choose the ith character of the 
        // second string if it exists 
        if (i < s2.length()) 
            result += s2[i];
    
    return result; 
  
// Driver code 
int main() 
    string s1 = "geeks"
    string s2 = "forgeeks"
    cout << merge(s1, s2);
  
    return 0;
}
  
// This code is contributed by gp6

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to alternatively merge two strings
public class mergeString {
  
    // Function for alternatively merging two strings
    public static String merge(String s1, String s2)
    {
        // To store the final string
        StringBuilder result = new StringBuilder();
  
        // For every index in the strings
        for (int i = 0; i < s1.length() || i < s2.length(); i++) {
  
            // First choose the ith character of the
            // first string if it exists
            if (i < s1.length())
                result.append(s1.charAt(i));
  
            // Then choose the ith character of the
            // second string if it exists
            if (i < s2.length())
                result.append(s2.charAt(i));
        }
  
        return result.toString();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s1 = "geeks";
        String s2 = "forgeeks";
        System.out.println(merge(s1, s2));
    }
}

chevron_right


Output:

gfeoerkgseeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.