51.9k views
5 votes
JAVA

In this exercise, you are given a string and a word. You should return the index location of the SECOND instance of that word. If the word doesn’t exist twice, return -1.

Example:

findSecond("A very, very good day", "very") --> 8
findSecond("A very good day", "very") --> -1

1 Answer

7 votes

Answer:

Answer is, as always, in the attached screenshot!

Step-by-step explanation:

In order to solve this we need to find the index of the string the first time, which will allow us to specify that as a new starting index to find the index of the second occurrence of the string. (Starting from +1 to get out of the string).

Really appreciate helping someone out with Java (it is my favourite programming language and I would say I know quite a bit about it).

Hope this helps!

JAVA In this exercise, you are given a string and a word. You should return the index-example-1
User Giles Bathgate
by
5.1k points