Advertisement
constk

Lab_Files_Part2

Mar 3rd, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <string.h>
  4. #include <iostream>
  5.  
  6. typedef struct {
  7. char** text;
  8. size_t sentences = 20;
  9. } TEXT;
  10.  
  11. int getTextFromFile(TEXT*, FILE*, const char[40]);
  12. void showAllText(TEXT*);
  13. void showSentencesContains(TEXT*, char[80]);
  14. bool strCheck(const char*, const char*);
  15. int strStr(const char*, const char*);
  16.  
  17. int main()
  18. {
  19. FILE* file = NULL;
  20. TEXT* Text = new TEXT;
  21. char word[80];
  22. const char fileName[40] = "Text.txt";
  23.  
  24. printf("Please, input the word: ");
  25. scanf("%s", word);
  26.  
  27. if (getTextFromFile(Text, file, fileName))
  28. //showAllText(Text);
  29. showSentencesContains(Text, word);
  30. else
  31. puts("#ERROR: Can't read data from file");
  32.  
  33. system("pause");
  34. return 0;
  35. }
  36.  
  37. int getTextFromFile(TEXT* Text, FILE* file, const char fileName[40])
  38. {
  39. if ((file = fopen(fileName, "r")) == NULL)
  40. {
  41. perror("Can't open file to read");
  42. return 0;
  43. }
  44. else
  45. {
  46. Text->text = (char**)malloc(sizeof(char*)*Text->sentences);
  47. char c;
  48. char currentString[1000] = "";
  49. size_t i = 0;
  50. size_t j = 0;
  51. do
  52. {
  53. c = fgetc(file);
  54. if (c == '.' && i < Text->sentences)
  55. {
  56. currentString[j] = '\0';
  57. Text->text[i] = (char*)malloc(sizeof(char) * (j + 1));
  58. strcpy(Text->text[i], currentString);
  59. i++;
  60. j = 0;
  61. }
  62. else if (c == '.' && i == Text->sentences)
  63. {
  64. Text->sentences *= 2;
  65. Text->text = (char**)realloc(Text->text ,sizeof(char*)*Text->sentences);
  66.  
  67. currentString[j] = '\0';
  68. Text->text[i] = (char*)malloc(sizeof(char) * (j + 1));
  69. strcpy(Text->text[i], currentString);
  70. i++;
  71. j = 0;
  72. }
  73. else if (c == '\n' || c == '\0')
  74. {
  75. c = ' ';
  76. currentString[j] = c;
  77. j++;
  78. }
  79. else
  80. {
  81. currentString[j] = c;
  82. j++;
  83. }
  84. } while (c != EOF);
  85. fclose(file);
  86. return 1;
  87. }
  88. }
  89.  
  90. void showAllText(TEXT* Text)
  91. {
  92. for (int i = 0; i < Text->sentences; i++)
  93. {
  94. printf("%s\n", Text->text[i]);
  95. }
  96. }
  97.  
  98. void showSentencesContains(TEXT* Text, char word[80])
  99. {
  100. bool bingo = false;
  101. for (int i = 0; i < Text->sentences; i++)
  102. {
  103. for (int j = 0; ( j < strlen(Text->text[i]) - strlen(word) ) && !bingo; j++)
  104. {
  105. if ( strStr(Text->text[i], word) != -1)
  106. {
  107. printf("%s\n\n", Text->text[i]);
  108. bingo = true;
  109. }
  110. }
  111.  
  112. bingo = false;
  113. }
  114. }
  115.  
  116. bool strCheck(const char *p, const char *pattern)
  117. {
  118. for (; *pattern != '\0'; pattern++)
  119. {
  120. if (*p != *pattern)
  121. return false;
  122. p++;
  123. }
  124. return true;
  125. }
  126.  
  127. int strStr(const char *text, const char *pattern)
  128. {
  129. if (*pattern == '\0')
  130. return 0;
  131. else if (*text == '\0')
  132. return -1;
  133. int N = strlen(text);
  134. for (int i = 0; i < N; i++)
  135. {
  136. if (strCheck(text + i, pattern))
  137. return i;
  138. }
  139. return -1;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement