Results 1 to 2 of 2

Thread: Recursion tips?

  1. #1
    MojoeMAN is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Recursion tips?

    Hello, I was practicing on CodingBat and for the following exercise came up with the solution posted below. It works, but I can't help but feel that it's somewhat convoluted and inelegant. Does anyone have any tips on how to clean it up? Thanks in advance.

    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"

    Java Code:
    public String endX(String str) {
      String working = "";
      
      if (str.length() > 1){
        working = endX(str.substring(1));
        if (str.charAt(0) == 'x') {
          return (working+"x");
        }
        else return (str.charAt(0)+working);
      }
      else return str;
    }

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

Similar Threads

  1. i need some tips on this question
    By reeveliew in forum New To Java
    Replies: 13
    Last Post: 06-15-2010, 06:29 PM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  3. Tips needed
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 12-07-2009, 02:35 AM
  4. need some derby tips:
    By kulangotski in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2009, 06:09 PM
  5. Need some tips.
    By PVL268 in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 03:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •