Przejdź do głównej zawartości

String-2 > mixString

Given two strings, a and b, create a bigger string made of the first char of a, the first char of b, the second char of a, the second char of b, and so on. Any leftover chars go at the end of the result.


mixString("abc", "xyz") → "axbycz"
mixString("Hi", "There") → "HTihere"
mixString("xxxx", "There") → "xTxhxexre"

Mix String:
dwa stringi o roznej dlugosci
Musimy przejsc loopem przez oba i zlozyc nowy string . aby miec pewnosc ze iterujemy po wszystkich char w stringu wybieramy dluzszy. Konieczne zadeklarowanie zmiennej String str ktora bedzie przechowywala kolejne char



public String mixString(String a, String b){
    
    String str = "";
    int len = 0;

    if (a.length() >= b.length())
    {
        len = a.length();
    } else
        len = b.length();

    for (int i = 0; i < len; i++)
    {

        if (i < a.length())
        {
            str += a.charAt(i);
        }

        if (i < b.length())
        {
            str += b.charAt(i);
        }

    }
    return str;
}

Komentarze