Alternatively Merge two Strings in Java | Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver Code
def merge ( s1 , s2 ) : NEW_LINE INDENT result = " " NEW_LINE i = 0 NEW_LINE while ( i < len ( s1 ) ) or ( i < len ( s2 ) ) : NEW_LINE INDENT if ( i < len ( s1 ) ) : NEW_LINE INDENT result += s1 [ i ] NEW_LINE DEDENT if ( i < len ( s2 ) ) : NEW_LINE INDENT result += s2 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT s1 = " geeks " NEW_LINE s2 = " forgeeks " NEW_LINE print ( merge ( s1 , s2 ) ) NEW_LINE