For the terminally insane: Figure this one out.
Untested, about half an hour's work.
I'm sure somebody can pick this one apart.
#include <stdlib.h>
char *repstr(char *s, char *m, char *r, char *t)
{
size_t sl = 0, ml = 0, tl = 0;
if (!s || !m || !r || !*m) { return 0; }
while (s[sl]) {
if (m[ml] == 0) {
if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
tl += rl;
sl += ml;
ml = 0;
} else if (m[ml] != s[sl+ml]) {
if (t) { t[tl] = s[sl]; }
tl++;
sl++;
ml = 0;
} else {
ml++;
}
}
if (t) {
t[tl] = 0;
return t;
} else if (t = malloc(tl + 1)) {
return repstr(s, m, r, t);
} else {
return 0;
}
}
char *replace_string(char *str, char *match, char *replace)
{
return repstr(str, match, replace, 0);
}
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Willem:
1. I wish I'd answered this before, and I apologize on behalf of the
slobs,thugs and creeps in this newsgroup for the lack of initial
response to your submission
2. I was, and remain, very impressed by your solution and as I
created the source file you see below I was rooting for you: for had
it ran with my test suite, I would have handed the "Olympic gold
medal" that I've awarded myself in the Spinoza challenge to you.
3. But "one of the great tragedies in life is the murder of a
beautiful theory by a brutal gang of facts" for your code has failed
many tests. Since it's late I may have made myself a clerical error in
assembling the test package.
4. Nonetheless, this has the potential to be the BEST solution. I
understand (unlike the regs and thugs here) that this is not a jobsite
and that we're all on different clocks and time schedules, and respect
your decision not to test the code. If you have the time, fix it, or,
I will see if I can tomorrow morning. Yes, we can use recursion, I
think, and this is a BRILLIANT insight. You see, I don't confuse code
monkeyshines with intelligence. Don't get me started...
5. Here is the source file I made and the console output.
#include <stdlib.h>
#include <stdio.h>
#define TESTER(resultPtr, master, target, replacement, expected) \
{ \
printf("Replace \"%s\" by \"%s\" in \"%s\"\n", \
(target), (replacement), (master)); \
printf("Expect \"%s\":\n \"%s\"\n", \
(expected), \
resultPtr = replace_string((master), \
(target), \
(replacement))); \
printf("\n\n"); \
free(resultPtr); \
}
char *repstr(char *s, char *m, char *r, char *t)
{
size_t sl = 0, ml = 0, tl = 0;
size_t rl;
if (!s || !m || !r || !*m) { return 0; }
while (s[sl]) {
if (m[ml] == 0) {
if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
tl += rl;
sl += ml;
ml = 0;
} else if (m[ml] != s[sl+ml]) {
if (t) { t[tl] = s[sl]; }
tl++;
sl++;
ml = 0;
} else {
ml++;
}
}
if (t) {
t[tl] = 0;
return t;
} else if (t = malloc(tl + 1)) {
return repstr(s, m, r, t);
} else {
return 0;
}
}
char *replace_string(char *str, char *match, char *replace)
{
return repstr(str, match, replace, 0);
}
int main()
{
char *ptrResult;
printf("\nReplace\n\n\n");
TESTER(ptrResult,
"1111123bbb1111123bbb11123bb11111231111112111111123",
"111123",
"ono",
"1onobbb1onobbb11123bb1ono1111112111ono")
TESTER(ptrResult,
"bbb1111123bbbbb",
"111123",
"ono",
"bbb1onobbbbb")
TESTER(ptrResult,
"a stupid error",
"stupid error",
"miracle",
"a miracle")
TESTER(ptrResult,
"a stupid error",
"stupid",
"miracle",
"a miracle error")
TESTER(ptrResult,
"the stupid error",
"the stupid error",
"a miracle",
"a miracle")
TESTER(ptrResult,
"the miracle",
"the",
"a",
"a miracle")
TESTER(ptrResult,
"a miraclsnirpKamunkle",
"snirpKamunkle",
"e",
"a miracle")
TESTER(ptrResult,
"a miraclesnirpKamunkle",
"a miracle",
"",
"snirpKamunkle")
TESTER(ptrResult,
" a miraclesnirpKamunkle",
"a miracle",
"",
" snirpKamunkle")
TESTER(ptrResult,
" a miraclesnirpKamunklea miraclea miracle",
"a miracle",
"",
" snirpKamunkle")
TESTER(ptrResult,
"a stupid errord",
"stupid error",
"miracle",
"a miracled")
TESTER(ptrResult,
"a stupid errod",
"stupid error",
"miracle",
"a stupid errod")
TESTER(ptrResult,
"a sstupid error",
"stupid error",
"miracle",
"a smiracle")
TESTER(ptrResult,
"a stupid errorstupid error",
"stupid error",
"miracle",
"a miraclemiracle")
TESTER(ptrResult,
"a stupid error stupiderror",
"stupid error",
"miracle",
"a miracle stupiderror")
TESTER(ptrResult,
"bbbbbbbbbb",
"b",
"a",
"aaaaaaaaaa")
TESTER(ptrResult,
"In the halls of R'yleh great %s lies dreaming",
"%s",
"Cthulu",
"In the halls of R'yleh great Cthulu lies dreaming")
TESTER(ptrResult,
"%s%s%s%s%s%s",
"%s",
"Cthulu",
"CthuluCthuluCthuluCthuluCthuluCthulu")
TESTER(ptrResult,
"banana",
"ana",
"oat",
"boatna")
TESTER(ptrResult,
" a stupid errorstupid errorHeystupid errors",
"stupid error",
"+",
" a ++Hey+s")
TESTER(ptrResult,
"foo barfoo barf",
"foo bar",
"bas",
"basbasf")
TESTER(ptrResult,
"abab",
"ba",
"ba",
"abab")
TESTER(ptrResult,
"abab",
"bab",
"boop",
"aboop")
TESTER(ptrResult,
"banana",
"ana",
"ono",
"bonona")
TESTER(ptrResult,
"a",
"x",
"b",
"a")
TESTER(ptrResult,
"x",
"x",
"b",
"b")
TESTER(ptrResult,
"egregious",
"egregious",
"egregious",
"egregious")
TESTER(ptrResult,
"egregious",
"egregious",
"x",
"x")
TESTER(ptrResult,
"",
"egregious",
"x",
"")
printf("\n\nTesting complete: check output carefully\n\n");
return 0;
}
Replace
Replace "111123" by "ono" in
"1111123bbb1111123bbb11123bb11111231111112111111123"
Expect "1onobbb1onobbb11123bb1ono1111112111ono":
"ono"
Replace "111123" by "ono" in "bbb1111123bbbbb"
Expect "bbb1onobbbbb":
"ono1"
Replace "stupid error" by "miracle" in "a stupid error"
Expect "a miracle":
"miracle"
Replace "stupid" by "miracle" in "a stupid error"
Expect "a miracle error":
"miracle"
Replace "the stupid error" by "a miracle" in "the stupid error"
Expect "a miracle":
"a miracle"
Replace "the" by "a" in "the miracle"
Expect "a miracle":
"a miracle"
Replace "snirpKamunkle" by "e" in "a miraclsnirpKamunkle"
Expect "a miracle":
"e miracl"
Replace "a miracle" by "" in "a miraclesnirpKamunkle"
Expect "snirpKamunkle":
"snirpKamunkle"
Replace "a miracle" by "" in " a miraclesnirpKamunkle"
Expect " snirpKamunkle":
" snirpKamunkle"
Replace "a miracle" by "" in " a miraclesnirpKamunklea miraclea
miracle"
Expect " snirpKamunkle":
" snirpKamunkle"
Replace "stupid error" by "miracle" in "a stupid errord"
Expect "a miracled":
"miracle"
Replace "stupid error" by "miracle" in "a stupid errod"
Expect "a stupid errod":
"a stupid errod"
Replace "stupid error" by "miracle" in "a sstupid error"
Expect "a smiracle":
"miracle"
Replace "stupid error" by "miracle" in "a stupid errorstupid error"
Expect "a miraclemiracle":
"miracle"
Replace "stupid error" by "miracle" in "a stupid error stupiderror"
Expect "a miracle stupiderror":
"miracle"
Replace "b" by "a" in "bbbbbbbbbb"
Expect "aaaaaaaaaa":
"a"
Replace "%s" by "Cthulu" in "In the halls of R'yleh great %s lies
dreaming"
Expect "In the halls of R'yleh great Cthulu lies dreaming":
"Cthulu halls of R'yleh great "
Replace "%s" by "Cthulu" in "%s%s%s%s%s%s"
Expect "CthuluCthuluCthuluCthuluCthuluCthulu":
"Cthulu"
Replace "ana" by "oat" in "banana"
Expect "boatna":
"oat"
Replace "stupid error" by "+" in " a stupid errorstupid errorHeystupid
errors"
Expect " a ++Hey+s":
"+a "
Replace "foo bar" by "bas" in "foo barfoo barf"
Expect "basbasf":
"bas"
Replace "ba" by "ba" in "abab"
Expect "abab":
"ba"
Replace "bab" by "boop" in "abab"
Expect "aboop":
"boop"
Replace "ana" by "ono" in "banana"
Expect "bonona":
"ono"
Replace "x" by "b" in "a"
Expect "a":
"a"
Replace "x" by "b" in "x"
Expect "b":
"b"
Replace "egregious" by "egregious" in "egregious"
Expect "egregious":
"egregious"
Replace "egregious" by "x" in "egregious"
Expect "x":
"x"
Replace "egregious" by "x" in ""
Expect "":
""
Testing complete: check output carefully