Main Page → Problems → Solve a Problem
Exercise 12.12: indexOf
Write a recursive method indexOf that accepts two Strings as parameters and that returns the starting index of the first occurrence of the second String inside the first String (or -1 if not found). The table below lists several calls to your method and their expected return values. Notice that case matters, as in the last example that returns -1.
| Call | Value Returned |
|---|---|
indexOf("Barack Obama", "Bar") |
0 |
indexOf("Barack Obama", "ck") |
4 |
indexOf("Barack Obama", "a") |
1 |
indexOf("Barack Obama", "McCain") |
-1 |
indexOf("Barack Obama", "BAR") |
-1 |
Strings have an indexOf method, but you are not allowed to call it. You are limited to these methods:
| Method | Description |
|---|---|
equals(String other) |
returns true if the two Strings contain the same characters |
length() |
returns the int number of characters in the String |
substring(int fromIndex, int toIndex) |
returns a new String containing the characters from this String from fromIndex (inclusive) to toIndex (exclusive), or to the end of the String if toIndex is omitted |
You are not allowed to construct any structured objects other than Strings (no array, List, Scanner, etc.) and you may not use any loops to solve this problem; you must use recursion.
If you do not understand how to solve a problem or why your solution code doesn't work, please contact your TA or instructor.
If something seems wrong with the Practice-It system itself (errors, slow performance, incorrect problem descriptions/tests, etc.), please
contact us.
Is there a problem?
Contact a Practice-It administrator.
Show/Hide Description
Re-indent