Or login with:
#include <stdlib.h>
| void | bsearch (const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *)) |
#include <stdio.h> #include <stdlib.h> #include <string.h> char strings[][20] = {"apples", "grapes", "strawberries", "bananas"}; int main () { char item[20] = "strawberries"; // first make sure the strings are sorted qsort(strings, 4, 20, (int(*)(const void*, const void*))strcmp); // search for "strawberries" char *pos = (char*) bsearch(item, strings, 4, 20, (int(*)(const void*, const void*))strcmp); if (pos) printf("The string "%s" has been found.\n", pos); else printf("The string "%s" has not been found.\n", item); return 0; }Output:
The string "strawberries" has been found.You must login to leave a messge