Advertisement
Kalhnyxtakias

Untitled

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