Question: (Java: Replace word in String) How to make a Java method recursive when it has to replace a word in a string? I need a
(Java: Replace word in String) How to make a Java method recursive when it has to replace a word in a string?
I need a method that will take in a sentence and then find and replace all instances of a given word with another given word.
The method I wrote without recursion is:
private static String substitute(String line, String find, String replace) { String[] words = line.split(" "); for (int k = 0; k if (words[k].equals(matchWordCase(words[k], find))){ words[k] = matchWordCase(words[k], replace);} else{} String done = String.join(" ", words);
return done; }
How would I go about writing recursive code that would do the same thing?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
