Keep Finding That String

Difficulty: ★ ★ ☆ ☆

One of the most clever string functions is strstr(), which searches for one string within another. Because of its return value — a char pointer — it’s possible to call this function multiple times to continue searching through the string.

I first wrote about the strstr() function back in 2014. I also demonstrated (with lovely illustrations) how to call the function a second time in a follow-up post. But I left it at that. Now it’s time to pick up the slack and use the strstr() function to find all instances of needles in a haystack.

By the way, finding needles in a haystack has nothing to do with sewing needles. The old phrase refers to pine needles. Hay is food for horses. Pine needles are not. A modern comparison would be finding a twig in your spaghetti.

From that ancient Lesson in 2014, I’ve created the following code for you to use as a base for this month’s Exercise:

2025_03_01-Exercise.c

#include <stdio.h>
#include <string.h>

int main()
{
    char phrase[] = "Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?";
    char find[] = "the";
    char *found;

    puts(phrase);
    found = strstr(phrase,find);
    printf("Found '%s' at offset %d.\n",
            find,
            (int)(found-phrase)
          );

    return 0;
}

This code searches for the word "the" in the phrase[] array. It finds it only once, reporting the word and its offset. Here’s a sample run:

Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?
Found 'the' at offset 9.

The offset is calculated as the difference between the location returned from the strstr() function and the base of the phrase[] array, found-phrase. This value is typecast to an integer for output as the offset in characters.

The code doesn’t do any error-checking. When the strstr() function is unable to find a string, it returns NULL. That’s beside the point.

Your challenge is to modify the code presented to have the strstr() function continue to search the given string. Remember, the value the function returns is an offset (pointer) into the string where the first match is found. It’s possible to call the function a second time to continue the search. Here’s output from my solution:

Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?
Found 'the' at offset 9.
Found 'the' at offset 60.

If I modify my solution to search for only the letter combination “th”, I see the following output:

Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?
Found 'th' at offset 4.
Found 'th' at offset 9.
Found 'th' at offset 18.
Found 'th' at offset 34.
Found 'th' at offset 60.

Please try this exercise on your own before you check out my solution.

2 thoughts on “Keep Finding That String

  1. Nice exercise, but I think there is something missing in its specification:

    Should the modified program take the possibility of overlapping search results into account, or can it be assumed that the earliest position for a next match is after the last character of the currently matched substring?

    char phrase[] = “The Knights Who Say Niiii!”;
    char find[] = “ii”;

    A search for non-overlapping matches should return 2 results:
    Found ‘ii’ at offset 21.
    Found ‘ii’ at offset 23.

    If the possibility of overlapping matches was properly accounted for, the updated program would have to return 3 results:
    Found ‘ii’ at offset 21.
    Found ‘ii’ at offset 22.
    Found ‘ii’ at offset 23.

    (Sorry for being so nit-picky, but I think this requires clarification!)

  2. You’re not being nitpicky; it’s a perfect programming question. I’ll comment more when my solution is published.

Leave a Reply