fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct WordCollection
  6. {
  7. size_t NumWords;
  8. char **Words;
  9. };
  10.  
  11. void splitWord(char *str, struct WordCollection *wc)
  12. {
  13. char *c;
  14. char **currentWord;
  15.  
  16. c = str;
  17.  
  18. wc->NumWords = 1;
  19.  
  20. while (*c != '.')
  21. {
  22. if (*c == ' ')
  23. {
  24. wc->NumWords++;
  25. }
  26.  
  27. c++;
  28. }
  29.  
  30. *c = '\0';
  31.  
  32. wc->Words = (char**)malloc(wc->NumWords * sizeof(char*));
  33.  
  34. c = strtok(str, " ");
  35.  
  36. currentWord = wc->Words;
  37.  
  38. while (c)
  39. {
  40. *currentWord = c;
  41. currentWord++;
  42.  
  43. c = strtok(NULL, " ");
  44. }
  45. }
  46.  
  47. int myComp(const void *p1, const void *p2)
  48. {
  49. return strcmp(*(const char**)p1, *(const char**)p2);
  50. }
  51.  
  52. int main(void)
  53. {
  54. char a[] = { 'w', 'o', 'r', 'n', 'g', ' ', 'w', 'o', 'r', 'd', '.' };
  55. char b[] = { 'c', 'o', 'r', 'r', 'e', 'c', 't', ' ', 'w', 'o', 'r', 'd', '.' };
  56.  
  57. struct WordCollection a1, b1;
  58. struct WordCollection *pSmaller, *pBigger;
  59.  
  60. size_t i;
  61.  
  62. splitWord(a, &a1);
  63. splitWord(b, &b1);
  64.  
  65. if (a1.NumWords <= b1.NumWords)
  66. {
  67. pSmaller = &a1;
  68. pBigger = &b1;
  69. }
  70. else
  71. {
  72. pSmaller = &b1;
  73. pBigger = &a1;
  74. }
  75.  
  76. qsort(pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);
  77.  
  78. for (i = 0; i < pSmaller->NumWords; i++)
  79. {
  80. void *res = bsearch(&pSmaller->Words[i], pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);
  81. if (res)
  82. {
  83. printf("Found: %s", pSmaller->Words[i]);
  84. }
  85. }
  86.  
  87. free(a1.Words);
  88. free(b1.Words);
  89.  
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Found: word