c - C 中的字符串搜索

标签 c arrays

在下面的代码中,strstr() 函数似乎没有正确响应 - if() 语句没有运行,因为 strstr() 始终给出空指针,尽管存在输入字符串(变量 search_for)在数组中。经过几个小时的挫折,我现在已经放弃了。请帮忙!

#include <stdio.h>
#include <string.h>
char tracks[][80] = {
    "impossible",
    "how to grow strong",
    "love is in the air",
    "3 prophets of everland",
    "home"
};
void find_track(char search_for[])
{
    int i;
    for (i = 0; i < 5; i++)
    {
        if (strstr(tracks[i], search_for))
            printf("Track %i: %s", i, tracks[i]);
    }
}
int main(void)
{
    char search_for[80];
    printf("Search for: \n");
    fgets(search_for, 80, stdin);
    printf("%s", search_for);
    find_track(search_for);

    return 0;
}

最佳答案

您的输入字符串包含换行符 \n。您可以使用调试器来跳过数小时的挫败感。

关于c - C 中的字符串搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25987029/

相关文章:

c++ - 在使用时以编程方式弹出 USB 设备

c++ - CUDA 设备代码中的 constexpr 数组

javascript hackerranks sherlock 和数组性能问题

ios - 字典 pList 的数组仅返回数组的索引

c - 需要在数组中找到最大、最小、平均数 - 没有声明大小

javascript - 你有比下面的代码更聪明的算法吗

c - 使用 GCC 链接器强制执行 32 位枚举

c++ - Ant 在长方体中找到最短路径 : length, 给出了长方体的宽度和高度。输出应以浮点形式显示最短距离

c++ - 循环显示0

C 新手 : Help with Simple Function