Avatar of lastcall1234
lastcall1234
 asked on

Java recursive method that returns a substring of the nth characters to the end of the string

I need to write a recursive Java method that returns a string formed by moving a substring of the first n characters to the end of the string.  I'm just learning recursive methods however I can't figure out how to move only part of a string to the end. 

For example:
moveCharsToBack (“hello”, 2) returns “llohe”.
moveCharsToBack (“hello”, 4) would return “ohell”.
moveCharsToBack (“hello”, 5) would return “hello”.
moveCharsToBack (“hello”, 6) would return “elloh”. 


This is the direction I was heading down but it doesn't even seem to be working for returning the first part correctly.  It doesn't work when you pass in int of 5 or 6.


   public static void main(String[] args) {
   
      System.out.println("moveCharsToBack  == " + moveCharsToBack("Hello", 2));
      System.out.println("moveCharsToBack  == " + moveCharsToBack("Hello", 4));
      System.out.println("moveCharsToBack  == " + moveCharsToBack("Hello", 5));
      System.out.println("moveCharsToBack  == " + moveCharsToBack("Hello", 6));
   
   }

   
   public static String moveCharsToBack(String input, int n) {
      if (input.isEmpty()) {
         return input;
      }
      String arr = input.substring(0, n);
      String firstPart = input.substring(n, input.length());
      String remainingPart;

      if (arr.length() == n) {
         remainingPart = arr;
      }

      else
         remainingPart = "";
      return moveCharsToBack(arr.substring(1), n - 1) + firstPart;
   }

}


Java

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
lastcall1234

ASKER
 I think I have the firstCharacter logic correct.  However I'm still not understanding how to build the modifiedInput correctly to produce the desired output.

public static String moveCharsToBack(String input, int n) {
       if (input.isEmpty() || n == 0 ) {
           return "";
       }
       else
       {
           firstCharacter = input.charAt(0);    
          modifiedInput = input.substring(1) + firstCharacter;
               
           return moveCharsToBack(modifiedInput, n - 1) + firstCharacter;
       }
ste5an

You need to copy the string starting at postition 2 using substring().
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy