fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. struct Node
  7. {
  8. int frequency;
  9. char word[50];
  10. struct Node *next;
  11. };
  12.  
  13. char *getWord(FILE *frequency)
  14. {
  15. char word[50];
  16. int ch, i = 0;
  17.  
  18. while (EOF != (ch = fgetc(frequency)) && !isalpha(ch)); //skip
  19.  
  20. if (ch == EOF)
  21. {
  22. return NULL;
  23. }
  24. do
  25. {
  26. word[i++] = tolower(ch);
  27. }
  28. while (EOF != (ch = fgetc(frequency)) && isalpha(ch));
  29.  
  30. word[i] = '\0';
  31. return strdup(word);
  32. }
  33.  
  34. unsigned long hash(char *str)
  35. {
  36. unsigned long hash = 5381;
  37. int c;
  38.  
  39. while ((c = *str++))
  40. {
  41. hash = ((hash << 5) + hash) + c;
  42. }
  43. return hash;
  44. }
  45.  
  46. int searchWord(struct Node *head, char *newWord)
  47. {
  48. int found = 0;
  49.  
  50. while (head != NULL)
  51. {
  52. if (strcasecmp(head->word, newWord) == 0)
  53. {
  54. found = 1;
  55. head->frequency = head->frequency + 1;
  56. return found;
  57. }
  58.  
  59. head = head->next;
  60. }
  61. return found;
  62. }
  63.  
  64. void insertWord(struct Node **head, char *newWord)
  65. {
  66. if ((*head) == NULL)
  67. {
  68. struct Node *ptr = malloc(sizeof (struct Node));
  69. if (ptr == NULL)
  70. {
  71. printf("\nError: Memory assignment error.\n");
  72. return;
  73. }
  74. ptr->frequency = 1;
  75. strcpy(ptr->word, newWord);
  76. ptr->next = NULL;
  77.  
  78. (*head) = ptr;
  79.  
  80. return;
  81. }
  82.  
  83. else if (searchWord(*head, newWord) == 1)
  84. {
  85. // Frequency is incremented in the Search Function
  86. return;
  87. }
  88. else
  89. {
  90. struct Node *ptr = malloc(sizeof (struct Node));
  91. if (NULL == ptr)
  92. {
  93. printf("\nError: Memory assignment error.\n");
  94. return;
  95. }
  96. ptr->frequency = 1;
  97. strcpy(ptr->word, newWord);
  98. ptr->next = NULL;
  99.  
  100. ptr->next = (*head);
  101. (*head) = ptr;
  102.  
  103. return;
  104. }
  105. }
  106.  
  107. void swap(struct Node *a, struct Node *b)
  108. {
  109. int temp = a->frequency;
  110. a->frequency = b->frequency;
  111. b->frequency = temp;
  112.  
  113. char tempS[50];
  114. strcpy(tempS, a->word);
  115. strcpy(a->word,b->word);
  116. strcpy(b->word,tempS);
  117. }
  118.  
  119. void sortList(struct Node *start)
  120. {
  121. int swapped;
  122. struct Node *ptr1;
  123. struct Node *lptr = NULL;
  124.  
  125. if (ptr1 == NULL)
  126. {
  127. return;
  128. }
  129. do
  130. {
  131. swapped = 0;
  132. ptr1 = start;
  133.  
  134. while (ptr1->next != lptr)
  135. {
  136. if (ptr1->frequency < ptr1->next->frequency)
  137. {
  138. swap(ptr1, ptr1->next);
  139. swapped = 1;
  140. }
  141. ptr1 = ptr1->next;
  142. }
  143. lptr = ptr1;
  144. }
  145. while (swapped);
  146. }
  147.  
  148. void printList(struct Node *head)
  149. {
  150. int counter = 0;
  151. printf("\n\nWord \t\tOccurrences\n");
  152. printf("---------------------------\n");
  153. if (head != NULL)
  154. {
  155. while (head && counter < 1000)
  156. {
  157. printf("%-20s %i\n", head->word, head->frequency);
  158. head = head->next;
  159. counter++;
  160. }
  161. }
  162. }
  163.  
  164. void freeLink(struct Node *head)
  165. {
  166. struct Node* current = head;
  167. while (current != NULL)
  168. {
  169. struct Node* next = current->next;
  170. free(current);
  171. current = next;
  172. }
  173. current = NULL;
  174. }
  175.  
  176. int main(int argc, char** argv)
  177. {
  178. FILE *data;
  179.  
  180. struct Node *head = NULL;
  181. char *word;
  182.  
  183. if (argc <= 2)
  184. {
  185. printf("Please Re-Run. Enter FileName and Optional Hashing (0/1).\n");
  186. exit(EXIT_FAILURE);
  187. }
  188. else if (argc == 3)
  189. {
  190. data = fopen(argv[1], "r");
  191.  
  192. if (data != NULL && (int)(argv[2][0] - '0') == 0)
  193. {
  194. while ((word = getWord(data)) != NULL)
  195. {
  196. insertWord(&head, word);
  197. }
  198. }
  199. else if (data != NULL && (int)(argv[2][0] - '0') == 1)
  200. {
  201. while ((word = getWord(data)) != NULL)
  202. {
  203. sprintf(word, "%lu", hash(word));
  204. insertWord(&head, word);
  205. }
  206. }
  207. else
  208. {
  209. printf("\nError: Unable to open file or invalid option\n");
  210. }
  211. fclose(data);
  212. }
  213. else
  214. {
  215. printf("\nError: Too Many Arguments\n");
  216. }
  217.  
  218. // Sorting
  219. sortList(head);
  220.  
  221. // Printing
  222. printList(head);
  223.  
  224. // Free Links
  225. freeLink(head);
  226. }
  227.  
  228.  
Runtime error #stdin #stdout 0s 2012KB
stdin
Standard input is empty
stdout
Please Re-Run. Enter FileName and Optional Hashing (0/1).