Prefix function and KMP search

Find every place a word occurs in a text. When only part of the word matches, the letters you already read tell you where to continue.

Build a table of matching beginnings and endings of the pattern. On a mismatch it tells us how many letters we can keep. The text cursor only moves forward.