[英]strcasestr still not working
So I read through other questions and they were told to put #define _GNU_SOURCE
before any include and it would work but it doesn't work for me. 所以我通读了其他问题,他们被告知在任何包含之前放#define _GNU_SOURCE
它会起作用,但它对我不起作用。 I also tried adding #define _GNU_SOURCE char *strcasestr(const char *haystack, const char *needle);
我也尝试添加#define _GNU_SOURCE char *strcasestr(const char *haystack, const char *needle);
but still doesn't work. 但仍然无法正常工作。 I couldn't find anything else about this, maybe anyone can help? 我找不到任何其他相关信息,也许任何人都可以提供帮助? Thanks in advance. 提前致谢。
Error: implicit declaration of function 'strcasestr' 错误:函数'strcasestr'的隐式声明
/**
*
* Description: This is code for Lab 3 Task 2.
* Reads data from file and gives opportunity to search by cities
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
printf("Please input the city you want to find employees in:");
scanf("%s", input);
maxline = i;
for (i = 0; i <= maxline; i++) {
if (strcasestr(employee[i].city, input) != 0) { // PROBLEM
printf("%d %s %s %s\n", &employee[i].ID, employee[i].fn,
employee[i].ln, employee[i].city);
amount++;
}
}
printf("%d matches out of %d members", amount, maxline);
return 0;
}
The strcasestr
function isn't available as part of the standard Windows build environment. strcasestr
函数不可用作标准Windows构建环境的一部分。 It's not a part of the C standard library and ships only with certain platforms and build environments. 它不是C标准库的一部分,只与某些平台和构建环境一起发布。
You can, however, code up your own version. 但是,您可以编写自己的版本。 Here's a simple one based on the naive string matching algorithm. 这是一个基于天真字符串匹配算法的简单方法。 You can possibly do better using the Rabin-Karp, Boyer-Moore, or Knuth-Morris-Pratt algorithms: 您可以使用Rabin-Karp,Boyer-Moore或Knuth-Morris-Pratt算法做得更好:
char* myStrcasestr(const char* haystack, const char* needle) {
/* Edge case: The empty string is a substring of everything. */
if (!needle[0]) return (char*) haystack;
/* Loop over all possible start positions. */
for (size_t i = 0; haystack[i]; i++) {
bool matches = true;
/* See if the string matches here. */
for (size_t j = 0; needle[j]; j++) {
/* If we're out of room in the haystack, give up. */
if (!haystack[i + j]) return NULL;
/* If there's a character mismatch, the needle doesn't fit here. */
if (tolower((unsigned char)needle[j]) !=
tolower((unsigned char)haystack[i + j])) {
matches = false;
break;
}
}
if (matches) return (char *)(haystack + i);
}
return NULL;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.