简体   繁体   English

字符串匹配的C代码[Head First C]似乎不起作用

[英]C Code for String matching[Head First C] doesn't seem to work

#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;
    for (i = 0; i < 5; i++)
    {
        if (strstr(tracks[i], search_for))
        printf("Track %i: '%s'\n", i, tracks[i]);
    }

}
int main()
{
    char search_for[80];
    printf("Search for: ");
    fgets(search_for, 80, stdin);
    find_track(search_for);
    getch();
    return 0;

}

This is the code taken directly from Head First C. This doesn't work.On the other hand if I change the line in main 这是直接从Head First C获取的代码。这不起作用。另一方面,如果我更改main中的行

char search_for[80];

to

char *search_for = "town"

It gives me the expected result. 它给了我预期的结果。 I don't understand why it doesn't work.I understand that directly pasting the code and telling you to find errors is not very much acceptable here but i guess it is a very small piece of basic code so it will do. 我不明白为什么它不起作用,我知道直接粘贴代码并告诉您发现错误在这里不是很可接受,但我想它只是一小部分基本代码,因此可以做到。

Thanks 谢谢

The problem with the code above is that it doesn't account for the fact that fgets leaves the newline in the string . 上面的代码的问题是它没有考虑fgets将换行符留在字符串中的事实。 So when you type town and hit enter , you'll end up searching for "town\\n" . 因此,当您键入town并按enter ,您最终将搜索"town\\n"

A cheap way to solve this would be to fix the string after calling fgets 解决此问题的一种便宜方法是在调用fgets之后修复字符串

search_for[strlen(search_for) - 1] = 0;

As already pointed out by @cnicutar, fgets leaves the newline char (code 10) in the string. 正如@cnicutar指出的那样,fgets将换行符char(代码10)留在字符串中。 You can find this out by yourself by iterating over the char array and printing the character codes and looking the special chars up in an ASCII code table (http://www.asciitable.com/): 您可以通过遍历char数组,打印字符代码并在ASCII码表(http://www.asciitable.com/)中查找特殊字符来自己找出答案:

int main()
{
    int i;
    char search_for[80];
    printf("Search for: ");
    fgets(search_for, 80, stdin);
    scanf("%79s", search_for);

    // print index, char code, char itself
    for (i=0;i<80;i++) {
        printf("%d: %d %c\n", i, search_for[i], search_for[i]);
    }
    find_track(search_for);
    getch();

    return 0;
}

The output will be: 输出将是:

...
0: 116 t
1: 111 o
2: 119 w
3: 110 n
4: 10
5: 0 
...

You can use the function "scanf" and limit the number of read chars to 79 (1 char reserve for NULL char at the end of the string as the buffer holds max 80 chars). 您可以使用函数“ scanf”并将读取的字符数限制为79(在字符串末尾为NULL字符保留1个字符,因为缓冲区最多可容纳80个字符)。 See: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ for reference. 请参阅: http : //www.cplusplus.com/reference/clibrary/cstdio/scanf/以获取参考。

int main()
{
    char search_for[80];

    printf("Search for: ");
    scanf("%79s", search_for);

    find_track(search_for);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM