简体   繁体   English

使用 C 中的 strstr() function 打印包含 substring 的主字符串

[英]Print the main string that has the substring inside using strstr() function in C

I'm reading the book "Head First C", and in the chapter 2.5 page 94, the author shows this code:我正在阅读《Head First C》一书,在第 2.5 章第 94 页中,作者显示了以下代码:

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]);
     }
 }

This will search the input of the user in the following array of arrays:这将在 arrays 的以下数组中搜索用户的输入:

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"
};

main() function: main() function:

int main()
{
    char search_for[80];
    puts("Search for: ");
    fgets(search_for, 80, stdin);
    find_track(search_for);

    return 0;
}

Output example: Output 示例:

Search for: Iwo
Result:
Track 4: 'The girl from Iwo Jima'

The problem with this code is that the author says that it works, but it doesn't.这段代码的问题是作者说它有效,但它没有。

The man page of strstr() says that this function will return a null pointer if the sub-string is not encountered in any strings of the array, and it will return a pointer of the sub-string to the beginning, so I did this adaptation to the code: strstr()的手册页说这个 function 如果在数组的任何字符串中都没有遇到子字符串,它将返回一个 null 指针,并且它将返回子字符串的指针指向开头,所以我这样做了适配代码:

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

    for (i = 4; i >= 0; --i) {
        temp = strstr(tracks[i], search_for);
        if (temp) {
            strcpy(temp, tracks[i]);
            printf("Track %i: '%s'\n", i, temp);
        } else {
            puts("Song was not found ... ");
            break;
        }
    }
}

What is wrong with the code?代码有什么问题? I did a bit of debugging, but everything seems to be fine, so maybe its a logic error, but with something as simple as the strstr() function, something like a logic mistake sounds hard to believe.我做了一些调试,但一切似乎都很好,所以它可能是一个逻辑错误,但是像strstr() function 这样简单的东西,类似逻辑错误的东西听起来很难相信。

Thanks,谢谢,

And as an extra secondary question... Is this a good book for a noob?作为一个额外的次要问题......这是一本适合菜鸟的好书吗?

fgets also reads newline character. fgets 还读取换行符。 You need to skip the new line character from the string before passing it to function.在将字符串传递给 function 之前,您需要跳过字符串中的换行符。 May be the below line will help:可能下面的行会有所帮助:

search_for[strcspn(search_for, "\n")]='\0';

strcspn gives no. strcspn 没有。 of characters of first parameter before it finds first occurrence of 2nd parameter in 1st parameter.在第一个参数中第一次出现第二个参数之前,第一个参数的字符数。 We are setting character at position of '\n' to '\0' which terminates the string at that position so characters after that position will be skipped.我们将 '\n' 的 position 处的字符设置为 '\0' ,这会终止 position 处的字符串,因此将跳过 position 之后的字符。

Regards, Haridas.问候,哈里达斯。

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

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