Alternatively Merge two Strings in Java - GeeksforGeeks

15 Dec, 2020
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++
Java
Python3
C#
// C++ code to alternatively merge two strings#include <bits/stdc++.h>using namespace std; // Function for alternatively merging two stringsstring 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 codeint main(){    string s1 = "geeks";    string s2 = "forgeeks";    cout << merge(s1, s2);     return 0;} // This code is contributed by gp6
// Java code to alternatively merge two stringspublic 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));    }}
# Python3 code to alternatively merge two strings # Function for alternatively merging two stringsdef merge(s1, s2):         # To store the final string    result = ""     # For every index in the strings    i = 0    while (i < len(s1)) or (i < len(s2)):                 # First choose the ith character of the        # first string if it exists        if (i < len(s1)):            result += s1[i]         # Then choose the ith character of the        # second string if it exists        if (i < len(s2)):            result += s2[i]                     i += 1             return result # Driver Codes1 = "geeks"s2 = "forgeeks" print(merge(s1, s2)) # This code is contributed by divyesh072019
// C# code to alternatively merge two stringsusing System;class GFG {         // Function for alternatively merging two strings    static 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;    }        static void Main() {        string s1 = "geeks";        string s2 = "forgeeks";        Console.WriteLine(merge(s1, s2));  }} // This code is contributed by divyeshrabadiya07
gfeoerkgseeks
 
shivamgupta356331
divyaramadoss1
gp6
divyesh072019
divyeshrabadiya07
Airtel
array-merge
Strings
Airtel
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to check if a string is palindrome or not
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters
How to split a string in C/C++, Python and Java?
Reverse words in a given string