Kalhnyxtakias

Untitled

Jan 14th, 2021
405
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void read_text(char word_array[][MAX_WORD_LEN], char * pointer_array[][2]) {
  2.     int j, word_count = 0, pointer_count = 0, k, same_word_found, EOF_check;
  3.     char word[MAX_WORD_LEN], format_string[20];
  4.  
  5.     sprintf(format_string,"%%%ds", MAX_WORD_LEN);
  6.  
  7.     do {
  8.         EOF_check = scanf(format_string, word);
  9.  
  10.         if (EOF_check == EOF) {
  11.             break;
  12.         }
  13.  
  14.         j = 0;
  15.  
  16.         /* Convert word to lowercase */
  17.         while (word[j] != '\0') {
  18.             word[j] = tolower(word[j]);
  19.             j++;
  20.         }
  21.  
  22.         same_word_found = 0;
  23.  
  24.         /* Compare currently read word with every word in the array */
  25.         /* If found in array, set pointer to the memory address of the word in the array */
  26.         for (k = 0; k < word_count; k++) {
  27.             if (!(strcmp(word, & word_array[k][0]))) {
  28.  
  29.                 pointer_array[pointer_count][0] = & word_array[k][0];
  30.  
  31.                 pointer_count++;
  32.  
  33.                 same_word_found = 1;
  34.  
  35.                 break;
  36.             } else {
  37.                 continue;
  38.             }
  39.         }
  40.  
  41.         /* If not found, add word to array and set pointer to memory address of newly added word */
  42.         if (!same_word_found) {
  43.             strcpy( & word_array[word_count][0], word);
  44.  
  45.             pointer_array[pointer_count][0] = & word_array[word_count][0];
  46.  
  47.             pointer_count++;
  48.  
  49.             word_count++;
  50.         }
  51.     } while (pointer_count < MAX_WORDS);
  52. }
RAW Paste Data