Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int cmp (const void * a, const void * b)
  7. {
  8.     return (strcmp(*(char**)a, *(char**)b));
  9. }
  10.  
  11. int main() {
  12.     char source[1001];
  13.     char patch[31];
  14.     fgets(source, 1001, stdin);
  15.     fgets(patch, 31, stdin);
  16.  
  17.  
  18.     source[strlen(source)]  = '\0';
  19.     //printf("%s\n", source);
  20.     patch[strlen(patch)]  = '\0';
  21.     //printf("============>>>>>>>>>%s\n", patch);
  22.  
  23.     char** arr = NULL;
  24.     int n = 0;
  25.     char * ptr = strtok(source, " .");
  26.     while (ptr != NULL)
  27.     {
  28.         arr = realloc(arr, (n+1) * sizeof(char*));
  29.         arr[n] = ptr;
  30.         //printf("%s\n", arr[n]);
  31.         n++;
  32.         ptr = strtok(NULL, " .");
  33.     }
  34.  
  35.     qsort(arr, n, sizeof(char*), cmp);
  36.     //printf("============\n");
  37.     //for(int i=0; i<n; i++)
  38.         //printf("{%s}\n",arr[i]);
  39.     char** patch_p = (char **) &patch;
  40.     ptr = (char*)bsearch(&patch_p, arr, n, sizeof(char*), cmp);
  41.     if(ptr)
  42.         printf("exists\n");
  43.     else
  44.         printf("doesn't exist\n");
  45.  
  46.     free(arr);
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement