Task 4 — Manual String Replacement
Objective
Create a function that manually replaces multiple string values inside a source string.
Replacement rules are provided as pairs.
The first value in each pair defines what should be searched for.
The second value defines the replacement.
Built-in string-replacement helpers must not be used.
Input
The source string is:
s1 := "abc bcacb cba acba abc bcacb cba acba abc bcacb qwe acb abc bcacb qwe acba"
The second argument is:
[]string
containing replacement pairs.
Conceptually:
search value
replacement value
search value
replacement value
...
Function
Create:
ReplaceValue(s1, replacements)
returning the modified:
string
Replacement Pair Format
For:
[]string{
"cba", "Alex",
"acba", "Ben",
"bcacb", "David",
}
the replacement rules are:
cba -> Alex
acba -> Ben
bcacb -> David
The number of elements in the replacement slice must therefore be even.
Matching Rule
Replacement is performed against complete space-separated values in the provided source.
This avoids ambiguous nested replacement behavior such as replacing "cba" inside "acba" before the explicit "acba" rule has a chance to match.
For this task, each whitespace-separated source value is treated as one token.
If the complete token matches a replacement key, replace that token.
Otherwise preserve it unchanged.
Case 1
Replacement definitions:
replacements := []string{
"cba", "Alex",
"acba", "Ben",
"bcacb", "David",
}
Call:
ReplaceValue(s1, replacements)
The rules are:
cba -> Alex
acba -> Ben
bcacb -> David
Expected Result
abc David Alex Ben abc David Alex Ben abc David qwe acb abc David qwe Ben
Case 2
Replacement definitions:
replacements := []string{
"abc", "Alex",
"qwe", "Ben",
"cba", "David",
}
Call:
ReplaceValue(s1, replacements)
The rules are:
abc -> Alex
qwe -> Ben
cba -> David
Expected Result
Alex bcacb David acba Alex bcacb David acba Alex bcacb Ben acb Alex bcacb Ben acba
Requirements
The function must:
- validate the replacement-pair definition
- process the source manually
- identify complete source tokens
- compare each token with the replacement keys
- append either the replacement or original token to the output
- preserve the original token order
- return the final string
Replacement Validation
The replacement collection must contain an even number of values.
For example:
[]string{
"abc", "Alex",
"cba",
}
is invalid because the final search value does not have a replacement partner.
Duplicate Search Keys
A replacement configuration should not contain the same search key more than once.
For example:
[]string{
"abc", "Alex",
"abc", "David",
}
is ambiguous.
A robust implementation should reject such configuration.
Manual Processing Restriction
Do not use APIs equivalent to:
Replace
ReplaceAll
Split
Fields
regular-expression replacement
when those APIs directly perform the operation required by the exercise.
Token boundaries and output construction should be implemented manually.
Why Token Matching Is Explicit
The original task provides several replacement keys where one value may be contained inside another.
For example:
cba
is contained inside:
acba
Treating the task as unrestricted substring replacement would therefore make replacement order affect the result.
This revised specification removes that ambiguity by defining replacement against complete whitespace-separated source values.
Implementation Notes
The algorithm may scan the input character by character and build each token manually.
When a token boundary is reached:
compare token with replacement keys
then append the appropriate value to the output.
The final result should not depend on the order in which replacement rules are stored.