Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

moveToEnd

Language/Type: Java recursion recursive programming
Author: Marty Stepp (on 2012/02/15)

Write a recursive method moveToEnd that accepts a String s and a char c as parameters, and returns a new String similar to s but with all occurrences of c moved to the end of the string. The relative order of the other characters should be unchanged from their order in the original string s. If s does not contain c, it should be returned unmodified.

The following table shows calls to your method and their return values.

Call Value Returned
moveToEnd("hello", 'l') "heoll"
moveToEnd("hello", 'e') "hlloe"
moveToEnd("hello there", 'e') "hllo threee"
moveToEnd("hello there", 'q') "hello there"
moveToEnd("HELLO there", 'e') "HELLO three"
moveToEnd("", 'x') ""

Obey the following restrictions in your solution:

  • You may not construct any structured objects other than Strings (no array, List, Scanner, etc.).
  • You may not call any string methods that perform bulk operations on the string object, such as split, indexOf, or replace. You may call methods that examine individual characters or divide a string into a smaller substring.
  • Your method must be recursive and not use any loops (for, while, etc.).
  • Your solution should run in no worse than O(N) time, where N is the number of characters in the string.
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.