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
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of lastcall1234
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;
       }
Avatar of ste5an
ste5an
Flag of Germany image

You need to copy the string starting at postition 2 using substring().
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo