strstr

I'm reading a book about C. Here is an example program.

Why doesn't strstr() find the "town" inside the tracks array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <string.h>

char tracks[][80] = {
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima"
};

void find_track(char search_for[])
{
    int i;
    char * pch;

    for (i = 0; i < 5; i++) {

        pch = strstr(tracks[i], search_for);

        printf("%p\n\n", pch);

        if (  pch   )
            printf("Track %i: '%s'\n", i, tracks[i]);
    }
}

int main()
{
    char search_for_m[80];
    printf("Search for: ");
    fgets(search_for_m, (sizeof(search_for_m)), stdin);
    printf("CHK mn --> Search for: %s\n\n", search_for_m);
    find_track(search_for_m);
    return 0;
}
Search for: town
CHK mn --> Search for: town


00000000

00000000

00000000

00000000

00000000


Process returned 0 (0x0)   execution time : 3.935 s
Press any key to continue.
http://en.cppreference.com/w/cpp/io/c/fgets
Reads at most count - 1 characters from the given file stream and stores them in str. The produced character string is always null-terminated. Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.

You are searching for "town\n" line. You might notice that there are 3 newlines between Search for: output and first pointer output istead of 2.
Thank you.
I didn't know about that: http://www.cplusplus.com/reference/cstdio/fgets/

Using C, which is better fgets() or scanf() or other?

char chr[80]; scanf("%s", chr); does not include \n into the string, or does it?
In your linK:
A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.


does not include \n into the string, or does it?
As newline is whitespace charcter, it is not read. Consider using width specifier to avoid overflow:
scanf("%79s", chr);
Thanks.
Removed '\n',
more precisely replaced '\n' to '\0',
it works as expected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <string.h>

char tracks[][80] = {
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima",
};

void find_track(char search_for[])
{
    printf("search_for: %s\n\n", search_for);

    int i;
    for (i = 0; i < 5; i++) {

        printf("%p\t", (strstr(tracks[i], search_for)));

        printf("tracks[%i]: %s\n", i, tracks[i]);

        if (  strstr(tracks[i], search_for)   )
            printf("\n\t\t ---> Track %i: '%s' <--- \n\n", i, tracks[i]);
    }
}

int main()
{
    char search_for_m[80];
    printf("Search for: ");
    fgets(search_for_m, 80, stdin);
    /*
     * It reads the '\n' at the end, when you hit ENTER, if input_length <=79.
     */
    char *nlp = strchr(search_for_m,'\n');
    if (nlp != NULL)
        *nlp = '\0';
    find_track(search_for_m);
    return 0;
}

Search for: town
search_for: town

00000000        tracks[0]: I left my heart in Harvard Med School
0040206D        tracks[1]: Newark, Newark - a wonderful town

                 ---> Track 1: 'Newark, Newark - a wonderful town' <---

00000000        tracks[2]: Dancing with a Dork
00000000        tracks[3]: From here to maternity
00000000        tracks[4]: The girl from Iwo Jima

Process returned 0 (0x0)   execution time : 3.612 s
Press any key to continue.
Topic archived. No new replies allowed.