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. I also tried adding #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'
/**
*
* 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. It's not a part of the C standard library and ships only with certain platforms and build environments.
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:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With