Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define MAX_WORDS 10
  6.  
  7. int comp(const void* word1, const void* word2){
  8.     printf("Comparing: %s %s\n", *(char**)word1, *(char**)word2);
  9.     return strcmp(*(char**)word1, *(char**)word2);
  10. }
  11.  
  12. int main(){
  13.     char text[1000] = "Java is";
  14.     char key[30] = "Java";
  15.     int cur_words = 0;
  16.     int max_words = MAX_WORDS;
  17.     char** words = malloc(max_words * sizeof(char *));
  18.     char* ptr = strtok(text, " .");
  19.     while(ptr != NULL){
  20.         if(cur_words == max_words){
  21.             max_words *= 2;
  22.             words = realloc(words, max_words * sizeof(char *));
  23.         }
  24.         words[cur_words] = ptr;
  25.         cur_words++;
  26.         ptr = strtok(NULL, " .");
  27.     }
  28.     qsort(words, cur_words - 1, sizeof(char*), comp);
  29.     printf("%d", bsearch(&key, words, cur_words - 1, sizeof(char*), comp) == NULL);
  30.     /*if(ind != NULL)
  31.         printf("exists\n");
  32.     else
  33.         printf("doesn' exist\n");
  34.     */
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement