the general method for a search& replace that I would use is something like: (written in a mix of code and text)
hope that's roughly intelligible
read "old" & "new" strings
create temp buffer of size strlen(old)+1
read in first strlen(old) characters to buffer
print new to output
load the next strlen(old) chars
write first char of buffer to output
shuffle all chars of buffer down one
read in next char from input to last char of buffer
we now have the whole of output printed, with the changes, except for the last (strlen(old)) characters, which are in the buffer.
Check if buffer == old, if so write new, otherwise just print buffer to output and close.