Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. ///\file proiect.c
  2. ///\brief C Text editor library
  3. ///
  4. /// Created by Badea Elena-Delia
  5. /// Implements functions for determinating if a word is present in a document, determinating the number of occurrences for a word in a document, replacing all instances of a word in a document with another given word, replacing specific instances of a word in a document with another given word, creating a sorted list of words by the number of occurrences in a document.
  6.  
  7. #include<stdlib.h>
  8. #include<string.h>
  9. #include<stdio.h>
  10. #include<malloc.h>
  11.  
  12. ///The variables ok and current_number are declared and initialized outside the functions
  13. int ok = 0 ;
  14. int current_number = 0;
  15.  
  16. int word(char given_word[256], char text_from_file[256])
  17. {
  18. ///\fn word(char given_word[256], char text_from_file[256])
  19. ///\brief This function is used to determinate if a word is present in a document, but it also counts the number of occurences of that word
  20. ///\param given_word The word we are looking for
  21. ///\param text_from_file The text from our file
  22.  
  23. char *cuv;
  24.  
  25. ///We start by dividing the text into words, using the strtok function
  26. cuv = strtok(text_from_file, " ,.!?;");
  27.  
  28. while(cuv)
  29. {
  30. if(strcmp(given_word, cuv)==0)
  31. {
  32. ///Every time we found the word, we increment the variable ok
  33. ok++;
  34. }
  35. cuv = strtok(NULL," ,.!?;" );
  36. }
  37.  
  38.  
  39. ///We return the number of appearences
  40. return ok;
  41.  
  42. }
  43.  
  44. char *replace(const char *s, const char *old, const char *new)
  45. {
  46. ///\fn char *replace(const char *s, const char *old, const char *new)
  47. ///\brief This function is used to replace every all instances of a word in our file with another one
  48. ///\param s The text where we are looking for the word and we replace it
  49. ///\param old The word we want to be replaced
  50. ///\param new The word we want to replace with
  51.  
  52. char *ret;
  53. int i, count = 0;
  54. ///The lenght of the new word
  55. size_t newlen = strlen(new);
  56. ///The lenght of the old word (the word we want to replace)
  57. size_t oldlen = strlen(old);
  58.  
  59. for (i = 0; s[i] != '\0'; i++)
  60. {
  61. ///We go through the text and we are looking for our word
  62. if (strstr(&s[i], old) == &s[i])
  63. {
  64. ///When we found the word, we increase the paramer count, used when we allocate memory for the new word
  65. count++;
  66. i += oldlen - 1;
  67. }
  68. }
  69.  
  70. ///We allocate memory for the new word
  71. ret = malloc(i + count * (newlen - oldlen));
  72.  
  73. if (ret == NULL)
  74. {
  75. exit(EXIT_FAILURE);
  76. }
  77.  
  78. i = 0;
  79. while (*s)
  80. {
  81. if (strstr(s, old) == s)
  82. {
  83. ///Here, the replacing is made, using the function strcpy, as long as we found the word in the text
  84. strcpy(&ret[i], new);
  85. i += newlen;
  86. s += oldlen;
  87. }
  88. else
  89. {
  90. ret[i++] = *s++;
  91. }
  92. }
  93.  
  94. ret[i] = '\0';
  95.  
  96. return ret;
  97. }
  98.  
  99. void new_word(FILE *f_2, char given_word[256], char rpwrd[256], char text_from_file[256])
  100. {
  101. ///\fn new_word(FILE *f_2, char given_word[256], char rpwrd[256], char text_from_file[256])
  102. ///\brief In this function we call the function replace and we replace the given word with another word in our text
  103. ///To do this, first we copy the text from the file in a variable, named here text_2. We do this because our text suffers changes and it can produce some errord to other functions
  104. ///Then we copy, using strcpy, in the text_2, the new text (made by calling the function replace) and after that we printf it in the file.
  105. ///\param f_2 Pointer to the file where we print the new text
  106. ///\param given_word The word we want to be replace
  107. ///\param rpwrd The word that we want to replace the given_word with
  108. ///\param text_from_file The text from our file
  109.  
  110. char text_2[256];
  111.  
  112. strcpy(text_2, text_from_file);
  113. strcpy(text_2,replace(text_2, given_word, rpwrd) );
  114. fprintf(f_2, "%s" ,text_2);
  115.  
  116.  
  117. }
  118.  
  119. char *replace_one_word(const char *s, const char *old, const char *new, int word_number)
  120. {
  121. ///\fn char *replace_one_word(const char *s, const char *old, const char *new, int word_number)
  122. ///\brief This function is like the function replace, but here, we replace only a specific instances of a word with another given word
  123. ///To do this, we count the appearences of the word and we replace only the specific inatance
  124. ///\param s The text
  125. ///\param old The word we want to be replaced
  126. ///\param new The word we want to replace with
  127. ///\param word_number The number of the appearance of the word that we want to replace
  128.  
  129. char *ret;
  130. int i, count = 0;
  131.  
  132. size_t newlen = strlen(new);
  133. size_t oldlen = strlen(old);
  134.  
  135.  
  136. for (i = 0; s[i] != '\0'; i++)
  137. {
  138. if (strstr(&s[i], old) == &s[i])
  139. {
  140. count++;
  141. i += oldlen - 1;
  142. }
  143. }
  144.  
  145. ret = malloc(i + (newlen - oldlen));
  146.  
  147. if (ret == NULL)
  148. {
  149. exit(EXIT_FAILURE);
  150. }
  151.  
  152. i = 0;
  153. while (*s)
  154. {
  155. if (strstr(s, old) == s )
  156. {
  157. current_number++;
  158. if(current_number == word_number)
  159. {
  160. ///We only replace the word when we are on the word_number-th appearence
  161. strcpy(&ret[i], new);
  162. i += newlen;
  163. s += oldlen;
  164. }
  165. else{ret[i++] = *s++;}
  166.  
  167. }
  168. else
  169. {
  170. ret[i++] = *s++;
  171. }
  172. }
  173.  
  174. ret[i] = '\0';
  175.  
  176. return ret;
  177. }
  178.  
  179. void replace_a_word(FILE *f, char given_word[256], char rpwrd[256], char text_from_file[256], int nr)
  180. {
  181. ///\fn replace_a_word(FILE *f, char given_word[256], char rpwrd[256], char text_from_file[256], int nr)
  182. ///\brief This function is used to replace a specific instance of a word with another given one
  183. ///To do this, we copy, using the function strcpy, our text from the file in the variable text, and then, using the function replace_one_word, we copy in variable text the new text obtained by calling the function replace_one_word
  184. ///Then we print the new text in the file.
  185. ///\param f Pointer to the file
  186. ///\param given_word The word we want to be replace
  187. ///\param rpwrd The word that we want to replace the given_word with
  188. ///\param text_from_file The text from our file
  189. ///\param nr The number of the appearence of the word we want to replace
  190.  
  191. char text[256];
  192. strcpy(text, text_from_file);
  193. strcpy(text,replace_one_word(text, given_word, rpwrd, nr) );
  194. fprintf(f, "%s" ,text);
  195.  
  196.  
  197. }
  198.  
  199. void sort(int *a[100], int *b[100], int n)
  200. { ///\fn void sort(int a[100], char *b[100])
  201. ///\brief This function is used to sort the two arrays, based on array a
  202. ///\param a A random array
  203. ///\param b A random array of strings
  204.  
  205. int i,j, aux_a, aux_b;
  206.  
  207. for(i=0; i<n; i++)
  208. {
  209. for(j=0; j<n; j++)
  210. {
  211. if(a[i] > a[j])
  212. {
  213. aux_a = a[i]; ///Interchange in a
  214. a[i] = a[j];
  215. a[j] = aux_a;
  216. aux_b = b[i];
  217. b[i] = b[j];
  218. b[j] = aux_b;
  219. }
  220. }
  221. }
  222. }
  223.  
  224.  
  225. void cuv(FILE *f)
  226. { ///\fn void cuv(FILE *f)
  227. ///\brief This function is used to list the words from the file sorted by the number of their occurences in the text
  228. ///\param f File pointer
  229. int lenght = 0, occure[100], i , j, freq[100], k , ok;
  230. char *words, text[256], *words_array[256], *list[256];
  231.  
  232. while(!(feof(f)))
  233. {
  234. fgets(text, sizeof(text),f);
  235. words = strtok(text, " ,.!?;\n\r\t");
  236.  
  237. while(words)
  238. {
  239. words_array[lenght] =(char*)calloc(words, sizeof(words) + 1); ///store the words into an string array
  240. strcat(words_array[lenght], words);
  241. lenght++;
  242. words = strtok(NULL," ,.!?;\n\r\t" );
  243. }
  244. }
  245. for(i=0;i<lenght;i++)
  246. { k=0;
  247. for(j=0;j<lenght;j++)
  248. {
  249. if(strcmp(words_array[i], words_array[j]) == 0)
  250. {
  251. occure[i] = k+1; ///count the appearences
  252. k++;
  253. }
  254.  
  255. }
  256. freq[i] = i; ///memorize the position
  257.  
  258. }
  259.  
  260. sort(occure, freq, lenght);
  261. j = 0 ;
  262. printf("List of words: ");
  263. ok = 0;
  264. while(j<lenght)
  265. {
  266. printf("%s ", words_array[freq[j]]); ///We print the words sorted by their number of occurence
  267. j++;
  268.  
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement