Alternativamente, combine dos strings en Java

Dadas 2 strings, combínelas de forma alternativa, es decir, el primer carácter de la string final es el primer carácter de la primera string, el segundo carácter de la string final es el primer carácter de la segunda string y así sucesivamente. Y si una vez que llega al final de una string mientras queda otra string, agregue el resto de esa string a la string final 

Ejemplos: 

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

Publicación traducida automáticamente

Artículo escrito por Akshit1037 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *