← all problems

withNeighbors 🌶️🌶️

Write a method that takes two String parameters, text and target. Find where target first occurs inside text, and return a String containing target together with the one character immediately before it and the one character immediately after it. For example, withNeighbors("abcdefg", "cde") should return "bcdef" (the target "cde" plus the "b" before it and the "f" after it).

Preconditions: target does occur in text, and that it is never at the very beginning or the very end (so there is always a character on each side).