Java > Recursion-1 > endX (CodingBat Solution)

Problem:

Given a string, compute recursively a new string where all the lowercase 'x' chars have been moved to the end of the string.

endX("xxre") → "rexx"
endX("xxhixx") → "hixxxx"
endX("xhixhix") → "hihixxx"


Solution:

public String endX(String str) {
  if (str.equals("")) return str;
  if (str.charAt(0) == 'x') return endX(str.substring(1)) + 'x';
  else return str.charAt(0) + endX(str.substring(1));
}


1 comment :

  1. This is my solution : public String endX(String str) {

    if(str.length()==0) return "";

    if(str.charAt(0)=='x'){
    return endX(str.substring(1))+"x";
    }
    return str.charAt(0)+endX(str.substring(1));
    }

    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