← all problems

replaceOnce 🌶️🌶️

Write a method that takes three String parameters, text, target and replacement, and returns text with the first occurrence of target replaced by replacement. For example, replaceOnce("I like cats", "cats", "dogs") should return "I like dogs", and replaceOnce("aaa", "a", "b") should return "baa". Build it with indexOf and substring.

Precondition: target appears in text at least once.