logo Practice-It logo

replace

Language/Type: Java recursion recursive programming
Author: Stuart Reges (on 2014/02/13)

Write a recursive method called replace that takes a string and two characters as parameters and that returns a new string with all occurrences of the first character replaced by the second character. The table below indicates the result of various method calls:

        Method Call                                   Result
        ----------------------------------------------------------------------
        replace("to be or not to be", 'e', 'o');      "to bo or not to bo"
        replace("to be or not to be", 'o', '-');      "t- be -r n-t t- be"
        replace("to be or not to be", 'm', '!');      "to be or not to be"
        replace("Mississippi", 'i', 'e');             "Messesseppe"
        replace("hah!", 'h', '*');                    "*a*!"
        replace("Hah!", 'h', '*');                    "Ha*!"
        replace("", 'g', 'h');                        ""

You are restricted to the following String methods:

        charAt(index)                  returns the character at the given index
        length()                       returns the length of the string
        substring(fromIndex, toIndex)  returns a new string that is a substring
                                       of this string from startIndex
                                       (inclusive) to stopIndex (exclusive)
        substring(fromIndex)           Returns a new string that is a substring
                                       of this string from startIndex
                                       (inclusive) to the end of the String

For example, if a String s stores the text "hello", then:

        s.charAt(0) returns 'h'        s.substring(1, 3) returns "el"
        s.length() returns 5           s.substring(2) returns "llo"

You will be constructing new strings, but you are not allowed to construct any other structured objects (no array, ArrayList, StringBuilder, etc) and you may not use a while loop, for loop, or do/while loop to solve this problem; you must use recursion.

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.