所以我通读了其他问题,他们被告知在任何包含之前放#define _GNU_SOURCE它会起作用,但它对我不起作用.我也尝试添加#define _GNU_SOURCE char * strcasestr(const char * haystack,const char * needle);但仍然无法正常工作.我找不到任何其他相关信息,也许任何人都可以提供帮助?提前致谢.
错误:函数’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;
}
最佳答案 strcasestr函数不可用作标准Windows构建环境的一部分.它不是C标准库的一部分,只与某些平台和构建环境一起发布.
但是,您可以编写自己的版本.这是一个基于天真字符串匹配算法的简单方法.您可以使用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;
}