B
Ben Bacarisse
<Knuth-Morris-Pratt implementation snipped>CBFalconer said:Try this. It's somewhat simpler (and faster and bufferless).
/* And heres another throw -- binfsrch.c by CBF */
Only three problems with this:
(1) The OP is starting out in C. Do you think an implementation of
KMP is simpler than the one-char at a time match I suggested they try
to implement?
(2) It does not do what they said they were trying to do. They wanted
a search and replace and with your version of KMP it is tricky to
convert to a version that echos up to the start of a match. Not that
hard for us, but were you suggesting a beginner should try to do it?
(3) It is wrong -- specifically it invokes the dreaded UB by writing
outside the bounds of an array.