Question: Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text

Use recursion to implement a method

public static int indexOf(String text, String str)

that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text.

For example, s.indexOf("Mississippi", "sip") returns 6.

Hint: This is a bit trickier than Exercise E13.8, because you must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.


Data from Exercise E13.8

Use recursion to implement a method 

public static boolean find(String text, String str)

that tests whether a given text contains a string. For example, find("Mississippi", "sip") returns true.

Hint: If the text starts with the string you want to match, then you are done. If not, consider the text that you obtain by removing the first character.

Step by Step Solution

3.43 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

class HelloWorld pu... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

2106_61d6ac3467235_873092.pdf

180 KBs PDF File

Word file Icon

2106_61d6ac3467235_873092.docx

120 KBs Word File

Students Have Also Explored These Related Java Concepts Late Objects Questions!