How To: Use strstr() in C

This is another addition to my earlier standard C string function how to’s. I have written in the past about strtol ( string to long ), as well as, strcpy ( used for copying strings ). In this ‘how to’ I would like to focus on using the strstr function, which allows you to locate a substring within your string. In most cases I end up using the strstr function for just detecting if a value/string exists within a string, essentially as a boolean check, rather than doing a lot of string manipulation with it. In contrast to using the strstr function, there is also a companion function known as strcasestr, which will ignore whether the letters are upper or lower case, this becomes quite handy when you are simply checking for presence of a string. If case is relevant to you, be sure to use strstr.

Function Definition of strstr

The strstr function is provided by the standard C string header file and used as follows:

// Header include
#include <string.h>
 
// Function definition
char *strstr(const char *haystack, const char *needle);

As outlined, the function definition is somewhat self explanatory. The first argument of the strstr function is haystack, you may have guessed it, but haystack is the string you would like to search within – for English speakers there is a saying of finding a needle in a haystack, which is exactly how this function is laid out. The second argument of strstr is needle. You guessed it, this is the string you would like to find within the first argument, haystack.

Using strstr

Now that you know how strstr is defined, the important part is using it correctly. I mentioned earlier that I usually treat strstr much like a boolean return. To do so we need to outline the return values of strstr:

  • If strstr finds the needle in the haystack it will return a pointer to the beginning of the needle within the haystack
  • If strstr cannot find the needle (string) within your haystack (string to search in) then it will return NULL

So this means to use this as a boolean return value, simply check for NULL. Of course this only applies if you plan to use it in this manner.

Here is a basic example:

#include <stdio.h>
#include <string.h>
 
int main(int argc, char **argv) {
    char s1[] = "Erik was here.";
    char *ptr;
 
    if((ptr = strstr(s1, "Erik")) == NULL)
        fprintf(stderr, "Could not find Erik\n");
    else
        fprintf(stdout, "Found String: -%s-\n", ptr);
 
  return 0;
}

In the first if statement we check for NULL, if NULL we realize the string does not exist within the haystack. The else case will print out the value of the string returned by the strstr function. To see if the first NULL check will work, remove the presence of “Erik” from the s1[] and recompile. The output will then become the “Could not find Erik”. Try it out for yourself!

There you have it, a basic explanation and example of using the C strstr function.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *