mystery5
method
public static String replace(String str, String find, String replacement)
{
if(str.length() < find.length())
return str;
if(str.substring(0, find.length()).equals(find))
return replacement + replace(str.substring(find.length()), find, replacement);
else
return str.substring(0, 1) + replace(str.substring(1), find, replacement);
}
The method replaces all occurrences of find
with replacement
.
The base case and the conditional statement are the same as mystery4
/countMatches
. If str
starts with find
, the method is called again with find
removed from the beginning of str
. replacement
is appended to the beginning of result of the call. If str
does not start with find
, a single character is removed from str
(and replaced after the call).