Max
Max

Reputation: 51

Merge two strings letter by letter in Java?

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.

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;
}

Upvotes: 2

Views: 10848

Answers (8)

Evgeniy
Evgeniy

Reputation: 140

Here is my solution:

  public String mixString(String a, String b) {
  int minLength = 0;
  String combination = "";
  String end = "";
  
  if (a.length() < b.length()) {
    minLength = a.length();
    end = b.substring(minLength, b.length());
  } else {
    minLength = b.length();
    end = a.substring(minLength, a.length());
  }
  
  for (int i = 0; i < minLength; i++) {
    combination = combination + a.substring(i, i+1) + b.substring(i, i+1);
  }
  
  return combination + end;
  
}

Upvotes: 0

Simran jeet Singh
Simran jeet Singh

Reputation: 1

Here is my Approach, feedback is most welcome

void printPattern(String str, String str1){
    int length1 = str.length();
    int length2 = str1.length();
    int i =0;
    String output ="";
    while(length1 > 0 && length2 > 0){
         output += (str.charAt(i)) ;
         output += (str1.charAt(i)) ;
        length1-- ; length2-- ; i++;
    }
    System.out.println(output);
}

Upvotes: 0

Rares
Rares

Reputation: 1

public String mixString(String a, String b) {
  String c="";
  String left="";
  int min;
  if(a.length()>b.length()) {
      min=b.length();
     left=a.substring(b.length(),a.length());
  }
  else {
      min=a.length();
      left=b.substring(a.length(),b.length());
  }

  for(int i=0;i<min;i++) {
      c +=a.substring(i,i+1)+b.substring(i,i+1);

  }   
return c+left;            
}

Upvotes: 0

rm65453
rm65453

Reputation: 81

public String mixString(String a, String b) {
StringBuilder sb = new StringBuilder("");
int longer = 0;
if (a.length()>b.length()) longer = a.length();
else longer = b.length();
for (int i = 0; i <longer;i++){
if (i<a.length()) sb.append(a.substring(i,i+1));
if (i<b.length()) sb.append(b.substring(i,i+1));
}
return sb.toString();
}

Upvotes: 0

RakeshKirola
RakeshKirola

Reputation: 83

Here is my attempt::

public static String mixString1(String a,String b){

    String result="";

    int startValue = 0;
    int increment = 0;

    if(a.length()<=b.length()){

    for(int i=0;i<a.length();i++){

            result =  result+a.charAt(i)+b.charAt(i);

    }

    }

    else if(a.length()>=b.length()){

        for(int i=0;i<b.length();i++){

                result =  result+a.charAt(i)+b.charAt(i);

        }

        }


    if(a.length()<b.length()){

        startValue= a.length();
        increment = b.length();

    }

    else {

        startValue= b.length();
        increment = a.length();

    }

    if(a.length()<b.length()){

    for(int j=startValue;j<increment;j++){

        result = result+b.charAt(j);

    }
    }
    else if(a.length()>b.length()){

        for(int j=startValue;j<increment;j++){

            result = result+a.charAt(j);

        }


    }

    return result;
    }

Upvotes: 0

dansalmo
dansalmo

Reputation: 11686

Another approach using StringBuilder and char[]. Works as long as the first is longer than the second, which is guaranteed by the first method.

public String mixString(String a, String b) {
    if (b.length() > a.length())
        return mixString(new StringBuilder(b), a.toCharArray(), 0);
    return mixString(new StringBuilder(a), b.toCharArray(), 1);
}

public String mixString(StringBuilder ab, char[] b, int start) {
    int i = 0;
    for (char c : b)
        ab.insert(i++*2 + start, "" + c);
    return ab.toString();
}

Upvotes: 0

Sandy
Sandy

Reputation: 226

I used Merge sort approach to solve this problem. First I converted both the strings into their respective character arrays and then merged both the arrays and converted the array back to a string. You can find my code below,I have tested the code and it is working. Let me know if you have any questions..

public String merge(String leftStr, String rightStr) {  
        char[]left = leftStr.toCharArray();
        char[]right = rightStr.toCharArray();
        int nL = left.length;
        int nR= right.length;
        char[] mergeArr= new char[nL+nR];
        int i=0,j=0,k=0;
        int temp =0;
        while(i<nL && j<nR){

            if (temp==0){
                mergeArr[k]=left[i];
                i++;
                temp =1;
            }else{
                mergeArr[k]=right[j];
                j++;
                temp=0;
            }
            k++;            
        }
        while(i<nL){

            mergeArr[k]=left[i]; k++; i++;
        }
        while(j<nR){

            mergeArr[k]=right[j]; k++; j++;
        }


        return new String(mergeArr);

    }

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

You've got a workable approach, but you could significantly simplify it by using a single loop with two counters:

int apos = 0, bpos = 0;
while (apos != a.length() || bpos != b.length()) {
    if (apos < a.length()) m += a.charAt(apos++);
    if (bpos < b.length()) m += b.charAt(bpos++);
}

In this loop you will "make progress" on each step by advancing apos, bpos, or both. Once a string runs out of characters, its corresponding pos stops advancing. The loop is over when both pos reach their ends.

Note: When you need to append to a string in a loop, use StringBuilder.

Upvotes: 7

Related Questions