logo CodeStepByStep logo

substring

Language/Type: Java recursion recursive programming
Related Links:

Write a recursive method substring that accepts as parameters a string, a start index, and an ending index, and returns a specified substring of the string. Your method should return the substring that begins at the start index and that extends to the character just before the ending index. For example, the call of substring("hamburger", 4, 8) should return "urge".

The method should throw an IllegalArgumentException if the start index is negative or if the ending index is greater than the length of the string or if the start index is greater than the ending index. The method should return an empty string if the two indexes are equal.

Constraints: In implementing this method, you are restricted to the following string methods: charAt, equals, and length. Do not construct any structured objects other than strings (no list, StringBuilder, , etc). Do not use any loops; you must use recursion.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.