#K56592. Search and Replace
Search and Replace
Search and Replace
You are given a text string and two words: a word to search for and a replacement word. Your task is to replace every occurrence of the search word in the text with the replacement word, but only when the search word appears as a whole word (i.e., not as a substring of another word).
The problem is case-sensitive. For example, if the search word is hello, then only hello should be replaced, while Hello or HELLO should remain unchanged.
Formally, if a word in the text is exactly equal to A, it should be replaced by B. Otherwise, the word remains unchanged.
In mathematical notation, if you let \( w_i \) be the _i_-th word in the text and \( A \) be the search word, then the transformation is given by:
[ w'_i = \begin{cases} B, & \text{if } w_i = A\ w_i, & \text{otherwise} \end{cases} ]
After processing all words, output the modified text with words separated by a single space.
inputFormat
The input is read from stdin
and formatted as follows:
- An integer T representing the number of test cases.
- For each test case, three lines are provided:
- A text string (which may contain spaces).
- The word to be replaced (A).
- The replacement word (B).
You need to process each test case independently.
outputFormat
For each test case, print the modified text on a new line to stdout
.
3
the quick brown fox jumps over the lazy dog
the
a
hello world hello
hello
hi
one two one
one
three
a quick brown fox jumps over a lazy dog
hi world hi
three two three
</p>