Advertisement
Ne-Biolog

Untitled

May 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <malloc/malloc.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. bool isLetter(char c) {
  8. if(c >= 'a' && c <= 'z') return true;
  9. if(c >= 'A' && c <= 'Z') return true;
  10. return false;
  11. }
  12.  
  13. bool stringComparison(char *x, int sizeX, char *y, int sizeY) {
  14. if(sizeX != sizeY) return false;
  15. for(int i = 0; i < sizeX; ++i) {
  16. if(x[i] != y[i]) return false;
  17. }
  18. return true;
  19. }
  20.  
  21. void readTextFromFile(int * &lenOfWords, char ** &listOfWords, int &numberOfWordsInFileN1, FILE *file) {
  22. char *currWord = (char*)malloc(sizeof(char) * 100);
  23. char ch = 'q';
  24. while(true) {
  25. if(ch == EOF) break;
  26. ch = fgetc(file);
  27.  
  28. if(isLetter(ch)) {
  29. int currLen = 0;
  30. currWord[currLen] = ch;
  31. while(true) {
  32. ch = fgetc(file);
  33. if(isLetter(ch)) {
  34. currLen++;
  35. currWord[currLen] = ch;
  36. } else break;
  37. }
  38.  
  39. currLen++;
  40. numberOfWordsInFileN1++;
  41.  
  42. lenOfWords = (int*)realloc(lenOfWords, numberOfWordsInFileN1 * sizeof(int));
  43. lenOfWords[numberOfWordsInFileN1 - 1] = currLen;
  44.  
  45. listOfWords = (char**)realloc(listOfWords, numberOfWordsInFileN1 * sizeof(char*));
  46. listOfWords[numberOfWordsInFileN1 - 1] = (char*)malloc(sizeof(char) * (currLen + 1));
  47.  
  48. for(int i = 0; i < currLen; ++i) {
  49. listOfWords[numberOfWordsInFileN1 - 1][i] = currWord[i];
  50. }
  51. }
  52. }
  53.  
  54. /*for(int i = 0; i < numberOfWordsInFileN1; ++i) {
  55. printf("%d ", lenOfWords[i]);
  56. for(int j = 0; j < lenOfWords[i]; ++j) {
  57. printf("%c", listOfWords[i][j]);
  58. }
  59. printf("\n");
  60. }*/
  61.  
  62. }
  63.  
  64. void searchForDuplicateWords(int *lenOfWordsInFileN1, char **listOfWordsInFileN1, int numberOfWordsInFileN1, int *lenOfWordsInFileN2, char **listOfWordsInFileN2, int numberOfWordsInFileN2) {
  65. for(int i = 0; i < numberOfWordsInFileN1; ++i) {
  66. int numberOfRepetitions = 0;
  67. for(int j = 0; j < numberOfWordsInFileN2; ++j) {
  68. if(stringComparison(listOfWordsInFileN1[i], lenOfWordsInFileN1[i], listOfWordsInFileN2[j], lenOfWordsInFileN2[j])) {
  69. numberOfRepetitions++;
  70. }
  71. }
  72. for(int j = 0; j < lenOfWordsInFileN1[i]; ++j) {
  73. printf("%c", listOfWordsInFileN1[i][j]);
  74. }
  75. printf(" in file N2 = %d ", numberOfRepetitions);
  76. printf("\n");
  77. }
  78. }
  79.  
  80. int main ()
  81. {
  82.  
  83. int *lenOfWordsInFileN1 = NULL;
  84. char **listOfWordsInFileN1 = NULL;
  85. int numberOfWordsInFileN1 = 0;
  86.  
  87. int *lenOfWordsInFileN2 = NULL;
  88. char **listOfWordsInFileN2 = NULL;
  89. int numberOfWordsInFileN2 = 0;
  90.  
  91. FILE *file1 = fopen("input.txt", "r");
  92. FILE *file2 = fopen("output.txt", "r");
  93. readTextFromFile(lenOfWordsInFileN1, listOfWordsInFileN1, numberOfWordsInFileN1, file1);
  94. readTextFromFile(lenOfWordsInFileN2, listOfWordsInFileN2, numberOfWordsInFileN2, file2);
  95.  
  96. searchForDuplicateWords(lenOfWordsInFileN1, listOfWordsInFileN1, numberOfWordsInFileN1,
  97. lenOfWordsInFileN2, listOfWordsInFileN2, numberOfWordsInFileN2);
  98.  
  99.  
  100. fclose(file1);
  101. fclose(file2);
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement