The task
Write a program or function that given three strings A, B, C produces an output string where each instance of B in A has been recursively substituted with C.
Recursively substituting means repeating a substitution where at each step all non-overlapping instances of B in A (chosen greedily from left to right) are replaced with C until B is no more contained in A.
Input/Output
- You may use any of the default methods for I/O.
- Strings will contain only printable ASCII characters (and may contain any of them) .
Bwill never be an empty string, whileAandCmight be.- Strings are to be considered plaintext, you can't for example treat
Bas a Regex pattern. - Some combinations of inputs will never terminate. Your program can do anything in those cases.
Test cases
These are in the format: A/B/C\nOutput
Hello, world!/world!/PPCG
Hello, PPCG
Uppercase is up/up/down
Uppercase is down
ababababa/aba/ccc
cccbcccba
delete/e/{empty string}
dlt
{empty string}/no/effect
{empty string}
llllrrrr/lr/rl
rrrrllll
+-+-+-+/+-+/+
+
ababababa/aba/bada
badabbadbada
abaaba/aba/ab
abb
((())())())/()/{empty string}
)
Examples that don't terminate:
grow/ow/oow
loop/lo/lo
((())())())/()/\$\endgroup\$downpercase is down\$\endgroup\$