Brute-Force Substring Search
The idea behind brute-force substring search is to use two loops to search the haystack. The outer loop combs through each character of the haystack one at a time, and when it finds a character that matches the first character of the needle, an inner loop begins. Before we get to the inner loop, though, let me show you what I’m talking about.
Say that our needle is the string "bet" and the haystack is the string "flibbertigibbet". (Of course that’s a real word. A flibbertigibbet is an excessively talkative person.) We begin with two pointers, one that points to the needle’s first character, and the other that points to the haystack’s first character:
We check if the two characters match. Currently, they don’t, so ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access