Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAX_WORDS 10
- int comp(const void* word1, const void* word2){
- printf("Comparing: %s %s\n", *(char**)word1, *(char**)word2);
- return strcmp(*(char**)word1, *(char**)word2);
- }
- int main(){
- char text[1000] = "Java is";
- char key[30] = "Java";
- int cur_words = 0;
- int max_words = MAX_WORDS;
- char** words = malloc(max_words * sizeof(char *));
- char* ptr = strtok(text, " .");
- while(ptr != NULL){
- if(cur_words == max_words){
- max_words *= 2;
- words = realloc(words, max_words * sizeof(char *));
- }
- words[cur_words] = ptr;
- cur_words++;
- ptr = strtok(NULL, " .");
- }
- qsort(words, cur_words - 1, sizeof(char*), comp);
- printf("%d", bsearch(&key, words, cur_words - 1, sizeof(char*), comp) == NULL);
- /*if(ind != NULL)
- printf("exists\n");
- else
- printf("doesn' exist\n");
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement