Java > String-2 > mixString (CodingBat Solution)

Problem:

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"


Solution:

public String mixString(String a, String b) {
  int aLen = a.length();
  int bLen = b.length();
  int max = Math.max(aLen, bLen);
  String word = "";
  
  for (int i = 0; i < max; i++) {
    if (i <= aLen-1)
      word += a.substring(i,i+1);
    if (i <= bLen-1)
      word += b.substring(i,i+1);

  }
  return word;
}


38 comments :

  1. I invented the wheel...
    public String mixString(String a, String b) {
    char[] result = new char[a.length() + b.length()];
    int i = 0;
    int aK = 0;
    int bK = 0;
    while (i < result.length){
    if (aK < a.length()) {
    result[i] = a.charAt(aK);
    aK++;
    i++;
    }
    if (bK < b.length()) {
    result[i] = b.charAt(bK);
    bK++;
    i++;
    }
    }
    String res = new String(result);
    return res;
    }

    ReplyDelete
  2. public String mixString(String a, String b) {
    int stringALen = a.length();
    int stringBLen = b.length();

    int shortStr = 0;

    String newStr = "";

    if(stringALen >= stringBLen) shortStr = stringBLen;

    if(stringBLen >= stringALen) shortStr = stringALen;



    if(stringALen >= stringBLen){
    for(int i = 0; i < shortStr; i++ ){
    newStr += a.charAt(i);
    newStr += b.charAt(i);
    }

    newStr = newStr + a.substring(shortStr);
    }

    if(stringBLen > stringALen){
    for(int i = 0; i < shortStr; i++ ){
    newStr += a.charAt(i);
    newStr += b.charAt(i);
    }

    newStr = newStr + b.substring(shortStr);
    }
    return newStr;
    }

    ReplyDelete
  3. public String mixString(String a, String b) {
    String result = "";
    int i = 0;
    while (i<a.length()&&i<b.length()){
    result = result + a.charAt(i) + b.charAt(i);
    i++;
    }
    return result+a.substring(i)+b.substring(i);
    }

    ReplyDelete
    Replies
    1. it works perfectly.
      to use this code in other editor

      class mix
      {
      public String mixString(String a, String b) {
      String result = "";
      int i = 0;
      while (i<a.length()&&i<b.length()){
      result = result + a.charAt(i) + b.charAt(i);
      i++;
      }
      return result+a.substring(i)+b.substring(i);
      }
      public static void main(String[] args)
      {
      String a1;
      mix b1= new mix();
      a1=b1.mixString("he","lo");
      System.out.println(a1);
      }
      }

      Delete
  4. public String mixString(String a, String b) {
    int min = Math.min(a.length(), b.length());

    String result = "";
    String tail = "";

    for(int i = 0; i < min; i++){
    result = result + a.charAt(i) + b.charAt(i);
    }

    if(a.length() < b.length()){
    tail = b.substring(min, b.length());
    result = result + tail;
    }

    if(a.length() > b.length()){
    tail = a.substring(min, a.length());
    result = result + tail;
    }

    return result;

    }

    ReplyDelete
    Replies
    1. Wow. We used almost exact same logic

      String blank = "";
      int lenA = a.length();
      int lenB = b.length();
      int minLen = Math.min(lenA,lenB);

      for(int i=0; i=lenB){
      blank = blank + a.substring(lenB);
      }
      return blank;

      Delete
    2. Wrong paste

      public String mixString(String a, String b) {
      String blank = "";
      int lenA = a.length();
      int lenB = b.length();
      int minLen = Math.min(lenA,lenB);

      for(int i=0; i=lenB){
      blank = blank + a.substring(lenB);
      }
      return blank;

      }

      Delete
  5. String str=""; int i=0;
    if(a.length()<=0)
    {
    return b;
    }
    else if(b.length()<=0)
    {
    return a;
    }
    else if(a.length()<b.length())
    {
    for(i=0;i<a.length();i++)
    {
    str = str+ a.substring(i,i+1)+b.substring(i,i+1);
    }
    return str+b.substring(i);
    }
    else
    {
    for(i=0;i<b.length();i++)
    {
    str = str+ a.substring(i,i+1)+b.substring(i,i+1);
    }
    return str+a.substring(i);
    }

    ReplyDelete
  6. public String mixString(String a, String b) {
    String result = "";

    if (a.length()<=b.length())
    {
    for (int i=0; i<a.length(); i++)
    {
    result = result + a.charAt(i) + b.charAt(i);
    }
    result = result + b.substring(a.length(),b.length());
    }

    if (b.length()<a.length())
    {
    for (int i=0; i<b.length(); i++)
    {
    result = result + a.charAt(i) + b.charAt(i);

    }
    result = result + a.substring(b.length(),a.length());
    }

    return result;

    }

    ReplyDelete
  7. public String mixString(String a, String b) {
    String newString = "";
    int minLen = Math.min(a.length(),b.length());

    for(int i=0; i<minLen; i++){
    newString = newString + a.charAt(i) + b.charAt(i);
    }
    if(minLen == a.length())
    newString = newString + b.substring(minLen);
    if(minLen == b.length())
    newString = newString + a.substring(minLen);
    return newString;
    }

    ReplyDelete
  8. I really need to remember that the Math class exists ._.

    public String mixString(String a, String b) {
    String result = "";
    int lengthA = a.length();
    int lengthB = b.length();
    if(lengthA > lengthB){
    for(int x = 0 ; x < lengthB ; x++){
    result = result + a.substring(x,x+1);
    result = result + b.substring(x,x+1);
    }
    result = result + a.substring(lengthB);
    }
    else if(lengthA < lengthB){
    for(int x = 0 ; x < lengthA ; x++){
    result = result + a.substring(x,x+1);
    result = result + b.substring(x,x+1);
    }
    result = result + b.substring(lengthA);
    }
    else{
    for(int x = 0 ; x < lengthA ; x++){
    result = result + a.substring(x,x+1);
    result = result + b.substring(x,x+1);
    }
    }
    return result;
    }

    ReplyDelete
  9. public String mixString(String a, String b) {
    int alen = a.length();
    int blen = b.length();
    String newStr = "";

    if (alen == blen)
    {
    for (int i = 0 ; i < alen ; i++)
    {
    newStr = newStr + a.charAt(i) + b.charAt(i);
    }
    }

    if (alen < blen)
    {
    for (int i = 0; i < alen; i++)
    {
    newStr = newStr+ a.charAt(i) + b.charAt(i) ;
    }
    newStr= newStr + b.substring(alen,blen);
    }

    if (alen > blen)
    {
    for (int i = 0; i < blen; i++)
    {
    newStr = newStr + a.charAt(i) + b.charAt(i) ;
    }
    newStr= newStr + a.substring(blen,alen);
    }

    return newStr;
    }

    ReplyDelete
  10. public String mixString(String a, String b) {
    String s="";
    if(a.length()<=b.length())
    {
    for(int i=0;ib.length())
    {
    for(int i=0;i<b.length();i++)
    {
    s=s+a.charAt(i)+b.charAt(i);
    }
    s=s+a.substring(b.length(),a.length());
    }
    return s;

    }

    ReplyDelete
  11. public String mixString(String a, String b) {
    int l = 0;
    String res = "";
    String h = "";
    if (a.length() <= b.length()) {
    l = a.length();
    h = b.substring(l);
    } else {
    l = b.length();
    h = a.substring(l);
    }

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

    res += a.substring(i, i+1) + b.substring(i, i+1);

    }

    return res + h;
    }

    ReplyDelete
  12. get a star from coding bat:

    public String mixString(String a, String b) {
    int max, min;
    if(a.length() > b.length())
    {
    max = a.length();
    min = b.length();
    }
    else
    {
    max = b.length();
    min = a.length();
    }

    String ab = "";
    for(int c = 0; c < min; c++)
    {
    ab = ab + a.charAt(c) + b.charAt(c);
    }

    if(max == a.length())
    {
    for(int d = min; d < max; d++)
    {
    ab = ab + a.charAt(d);
    }
    }
    else
    {
    for(int d = min; d < max; d++)
    {
    ab = ab + b.charAt(d);
    }
    }

    return ab;
    }

    ReplyDelete
  13. public String mixString(String a, String b) {
    if(a.length()== 0)
    return b;
    else if(b.length() == 0)
    return a;
    else
    return "" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
    }

    ReplyDelete
  14. public String mixString(String a, String b) {

    String temp="";
    int minimum =Math.min(a.length(),b.length());

    for (int i=0;ib.length())
    temp+=a.substring(minimum);

    return temp;
    }

    ReplyDelete
  15. public String mixString(String a, String b) {

    String temp="";
    int minimum =Math.min(a.length(),b.length());

    for (int i=0;ib.length())
    temp+=a.substring(minimum);

    return temp;
    }

    ReplyDelete
  16. public String mixString(String a, String b) {
    int small = (a.length() > b.length() ? b.length() : a.length());
    String big = (a.length() > b.length() ? a : b);
    String result = "";
    for (int i = 0; i < small; i++) {
    result = result + a.charAt(i) + b.charAt(i);
    }
    return result + big.substring(small);
    }

    ReplyDelete
  17. public String mixString(String a, String b) {
    String result = "";
    int len = 0;
    if ( a.length() <= b.length()) {
    len = a.length();
    } else if ( a.length() > b.length()){
    len = b.length();
    }
    for ( int i = 0; i < len ; i++ ){
    result = result + a.charAt(i) + b.charAt(i);
    }
    if ( a.length() < b.length()) {
    result += b.substring(a.length());
    } else {
    result += a.substring(b.length());
    }

    return result;
    }

    ReplyDelete
  18. public String mixString(String a, String b) {
    int len = Math.min(a.length(), b.length());
    String word = "";
    for(int i=0; ib.length())
    {
    word = word + a.substring(len, a.length());
    }
    if(b.length()>a.length())
    {
    word = word + b.substring(len, b.length());
    }

    return word;


    }

    ReplyDelete
  19. public String mixString(String a, String b) {
    String word = "";
    int max = Math.max(a.length(),b.length());
    for(int i=0; i<max; i++){
    if(i<a.length())
    word+=a.charAt(i);
    if(i<b.length())
    word+=b.charAt(i);
    }
    return word;
    }

    ReplyDelete
  20. public String mixString(String a, String b) {
    String rep = "";
    for (int i = 0; i < Math.max(a.length(), b.length()); i++)
    {
    if (i < a.length())
    {
    rep += a.substring(i, i + 1);
    }
    if (i < b.length())
    {
    rep += b.substring(i, i + 1);
    }
    }
    return rep;
    }

    ReplyDelete
  21. public String mixString(String a, String b) {
    if (a.length() == 0){
    return b;
    }
    if (b.length() == 0){
    return a;
    }
    return "" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
    }

    ReplyDelete
  22. public String mixString(String a, String b) {
    return (a.length() == 0)? b:
    (b.length() == 0)? a:
    "" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
    }

    ReplyDelete
  23. public String mixString(String a, String b) {
    String mix = "";
    if(a.length()> b.length()){
    for(int i=0;i a.length()){
    for(int j=0;j <a.length(); j++){
    mix = mix + a.charAt(j) + b.charAt(j);
    }
    mix = mix + b.substring(a.length());
    } if(a.length() == b.length()){
    for(int i=0;i <b.length(); i++){
    mix = mix + a.charAt(i) + b.charAt(i);
    }

    }
    return mix;
    }

    ReplyDelete
  24. AAaauuuuugGgHhhhhh

    public String mixString(String a, String b) {
    if(a=="") return b;
    if(b=="") return a;

    int len = a.length()<b.length() ? a.length() : b.length();
    String res="";

    for(int i=0;i<len;i++){
    res+=a.substring(i,i+1)+b.substring(i,i+1);
    }

    if(a.length()<b.length()){
    if(a.length()*2==b.length()){
    res+=b.substring(b.length()-a.length());
    } else {
    res+=b.substring((b.length()%a.length())*a.length());
    }
    }
    if(b.length()<a.length()){
    if(b.length()*2==a.length()){
    res+=a.substring(a.length()-b.length());
    } else {
    res+=a.substring((b.length()%a.length())*b.length());
    }
    }
    return res;
    }

    ReplyDelete
  25. public String mixString(String a, String b) {

    String res = "";
    int alen = a.length();
    int blen = b.length();
    int min = Math.min(alen, blen);

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

    res += a.substring(i, i+1) + b.substring(i, i+1);
    }

    return (i < alen) ? (res += a.substring(i, alen)) : (res += b.substring(i, blen));
    }

    ReplyDelete
  26. public String mixString(String a, String b) {

    String result="";

    int minTall=Math.min(a.length(), b.length() );
    for(int i=0;ib.length())return a;
    else return b;}

    ReplyDelete
  27. public String mixString(String a, String b)
    {
    int xlen = a.length() ;
    int ylen = b.length() ;
    int max = Math.max(xlen, ylen) ;
    String Roomy = "" ;
    for(int i = 0 ; i < max ; i++)
    {
    if(i <= xlen-1)
    Roomy += a.substring(i,i+1);
    if(i <= ylen-1)
    Roomy += b.substring(i,i+1) ;

    }
    return Roomy ;

    }

    ReplyDelete
  28. public String mixString(String a, String b)
    {
    String s = "";

    for(int i =0; i < (a.length() + b.length()); i++)
    {
    if(a.length() > i) s = s + a.charAt(i);
    if(b.length() > i) s = s + b.charAt(i);
    }

    return s;
    }

    ReplyDelete
  29. public String mixString(String a, String b) {
    StringBuilder str = new StringBuilder();

    if(a.length() > 0 && a.length() <= b.length()){
    for(int i = 0; i < a.length(); i++){
    str.append(a.charAt(i));
    str.append(b.charAt(i));
    }
    str.append(b.substring(a.length(),b.length()));
    }

    else if(a.length() > 0 && b.length() >= b.length()){
    for(int i = 0; i < b.length(); i++){
    str.append(a.charAt(i));
    str.append(b.charAt(i));
    }
    str.append(a.substring(b.length(), a.length()));
    }

    else return b;

    return str.toString();
    }

    ReplyDelete
  30. public String mixString(String a, String b) {
    int longest = 0 ;
    if(a.length() >= b.length()) longest = a.length();
    else longest = b.length();

    String newStr = "" ;
    for(int x = 0; x < longest ; x++) {
    if (a.length() > x) newStr = newStr + a.charAt(x) ;
    if (b.length() > x) newStr = newStr + b.charAt(x) ;
    }
    return newStr ;
    }

    ReplyDelete
  31. String x= "";
    int alen = a.length();
    int blen = b.length();
    String sub = "";
    if(alen > blen) sub += a.substring(blen);
    else sub += b.substring(alen);
    for (int i = 0; i < alen; i++) {
    if(a.substring(i).isEmpty() || b.substring(i).isEmpty()) break;
    else x += a.charAt(i) +""+ b.charAt(i);
    }
    return x+=sub;

    ReplyDelete
  32. public String mixString(String a, String b) {
    int max=0,min=0;
    String str="";
    String maxx="";
    if(a.length()>=b.length()){
    max=a.length();
    min=b.length();
    maxx=a;
    }
    else{
    max=b.length();
    min=a.length();
    maxx=b;
    }
    for(int i=0;i0){
    str=str+a.charAt(i)+b.charAt(i);
    min--;
    }
    else{
    str=str+maxx.charAt(i);
    }
    }
    return str;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact