Write a program to replace all occurrences of "force" with "first" and all occurrences of "first" with "force", keeping the original case for all character positions:
"ForcefoRcefOrcE" -> "FirstfiRstfIrsT"
"FirstfiRstfIrsT" -> "ForcefoRcefOrcE"
The rest of the string must stay unchanged, and so running your program twice shall return the original string:
"thirst of forces" -> "thirst of firsts" -> "thirst of forces"
Your program should work on any initial string. So as a hint, you better avoid using magic characters as intermediate representation, because if you try a three pass replacement ("force" -> "zzzzz", "first" -> "force", "zzzzz" -> "first"
), it will fail on strings containing "zzzzz"
.
You should support the full range of characters allowed in a definition of a String by your programming language (in most cases, it's Unicode). Example, using JSON-style representation for non-printable characters (\u + 4 digits):
"\u0000\u0001\u0002\u0003the Force of the firsT"
|
V
"\u0000\u0001\u0002\u0003the First of the forcE"
[Ff]
isn't there then you must not replace the word. \$\endgroup\$ – Erik the Outgolfer May 1 '17 at 7:57