Alternatively Merge two Strings Last Updated : 23 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 : hgeelelkosRecommended PracticeMerge Two Strings AlternativelyTry It! The idea is simple, we create a result string. We one by one append characters of both given strings in alternate style. C++ // 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 Java // 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)); } } Python3 # Python3 code to alternatively merge two strings # Function for alternatively merging two strings def 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 Code s1 = "geeks" s2 = "forgeeks" print(merge(s1, s2)) # This code is contributed by divyesh072019 C# // C# code to alternatively merge two strings using 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 JavaScript // JavaScript code to alternatively merge two strings function mergeString(s1, s2) { // To store the final string let result = ""; // For every index in the strings for (let 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.charAt(i); // Then choose the ith character of the // second string if it exists if (i < s2.length) result += s2.charAt(i); } return result; } // Driver code let s1 = "geeks"; let s2 = "forgeeks"; console.log(mergeString(s1, s2)); //This code is Contributed by chinmaya121221 Outputgfeoerkgseeks Complexity Analysis: Time Complexity: O(max(L1,L2)), Where L1 and L2 are the lengths of string 1 and string 2 respectively.Auxiliary Space: O(L1+L2), Where L1 and L2 are the lengths of string 1 and string 2 respectively. Comment More infoAdvertise with us Next Article Alternatively Merge two Strings A Akshit1037 Follow Improve Article Tags : DSA Strings Practice Tags : Strings Similar Reads Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right 2 min read Hamming Distance between two strings You are given two strings of equal length, you have to find the Hamming Distance between these string. Where the Hamming distance between two strings of equal length is the number of positions at which the corresponding character is different. Examples: Input : str1[] = "geeksforgeeks", str2[] = "ge 4 min read Shorten and Match the String Given two strings S1 and S2, which consists of uppercase alphabets. Find the shortest string str which consists of # and uppercase letters, where the number of # must be less than the number of letters. Make sure if you replace the # in the string str with any string of uppercase letters of any leng 14 min read What is String - Definition & Meaning In Data Structures and Algorithms (DSA), a String can also be defined as a sequence of characters, stored in contiguous memory locations, terminated by a special character called the null character '\0'. StringCharacteristics of String: In the context of Data Structures and Algorithms, strings have 3 min read Check if two strings have a common substring You are given two strings str1 and str2. You have to check if the two strings share a common substring. Examples : Input : str1 = "HELLO" str2 = "WORLD" Output : YES Explanation : The substrings "O" and "L" are common to both str1 and str2 Input : str1 = "HI" str2 = "ALL" Output : NO Explanation : B 5 min read Minimize partitions in given string to get another string Given two strings A and B, print the minimum number of slices required in A to get another string B. In case, if it is not possible to get B from A, then print "-1". Examples : Input: A = "geeksforgeeks", B = "ksgek"Output: 5Explanation: g | ee | ks | forge | ek | s : minimum 5 slices are required t 15+ min read Minimizing Steps to Form Anagrams from Given Strings Given two strings s1 and s2. You have the flexibility to add any letter to either the string s1 or s2 in just one action. Find out the least number of steps needed to transform two given words, s1, and s2, into anagrams of each other. The length of both strings can be different and it contains only 7 min read Check if edit distance between two strings is one An edit between two strings is one of the following changes. Add a characterDelete a characterChange a characterGiven two strings s1 and s2, find if s1 can be converted to s2 with exactly one edit. Note: Even if the strings are already equal, one edit operation can still be considered valid by "chan 14 min read Java String join() with examples The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. :public stat 2 min read Check if two strings are permutation of each other Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, "abcd" and "dabc" are Permutation of each other. We strongly recommend that 13 min read Like