Learning C: fgets() and strstr()

I am currently reading Head First C and noticed that the following (slightly modified) example code is broken:

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

char names[][80] = {
    "David Bowie",
    "Julia Roberts",
    "George Bush",
    "Robin Hanson",

void find_name(char search_for[])
    int i;
    for (i = 0; i < 4; i++) {
        if (strstr(names[i], search_for)) {
            printf("Name %i: '%s'\n", i, names[i]);


int main()
    char search_for[80];
    printf("Enter search term: ");
    fgets(search_for, 80, stdin);


    return 0;

So what’s wrong? Well,

The fgets function reads characters from the stream up to and including a newline character and stores them in the string s, adding a null character to mark the end of the string.

A newline character makes fgets() stop reading and is included in the string. Which means that when you type “David Bowie” the search string will contain “David Bowie\n”, which does not equal names[0]:

{‘D’, ‘a’, ‘v’, ‘i’, ‘d’, ‘ ‘, ‘B’, ‘o’, ‘w’, ‘i’, ‘e’, ‘\n‘, ‘\0’} does not equal {‘D’, ‘a’, ‘v’, ‘i’, ‘d’, ‘ ‘, ‘B’, ‘o’, ‘w’, ‘i’, ‘e’, ‘\0’}

The solution? Replace the newline character by a NULL character:

size_t len = strlen(search_for) - 1;

if (search_for[len] == '\n');
    search_for[len] = '\0';

This works because the NULL character marks the end of any character array that is a string. In other words, the string ends at the first null character.

Tags: ,

Comments are now closed.