Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the Following code: /* ===================================================

ID: 3863919 • Letter: G

Question

Given the Following code:

/*
============================================================================
Description : A program demonstrating string handling.
The program reads text (with no punctuation except for contractions)
and produces an alphabetical list of the words in the text.
Capitalization is removed.
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 500
#define MAXC 50

int main(void) {
   char word[MAXC + 1], list[MAXW][MAXC + 1];
   int n, i, j, k;
   setvbuf(stdout, NULL, _IONBF, 0);
   n = 0;
   printf("Enter text with EOD to mark end: ");
   while (1) {
       scanf("%s", word);
       if (0 == strcmp("EOD", word))
           break;
       // change first letter to lower-case if word is capitalized
       if ('A' <= word[0] && 'Z' >= word[0])
           word[0] += 'a' - 'A';
       // go through list of words
       for (i = 0; i <= n; i++) {
           // if at end of list (i==n) add the new word
           if (i == n) {
               strcpy(list[n], word);
               n++;
               break;
           }
           // compare word to word in list at position i
           k = strcmp(word, list[i]);
           // when k==0 word is not a new word
           if (k == 0) {
               break;
           }
           // when k < 0 we want to insert word into the list at position i
           if (k < 0) {
               // move rest of list to make room
               for (j = n; j > i; j--)
                   strcpy(list[j], list[j - 1]);
               // insert new word at position i
               strcpy(list[i], word);
               n++;
               break;
           }
           // note if k > 0 we just want to go onto the next position in the list
       }
   }

   printf(" Word list: ");
   for (i = 0; i < n; i++)
       printf("%s ", list[i]);
   return EXIT_SUCCESS;
}

You are to modify the program so that it counts the number of times each word occurs. In the output, the number of occurrences of each word should appear in brackets after the word, e.g. aardvark (10) .

This is a problem in C programming. Thanks!

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 500
#define MAXC 50

int main(void) {
   char word[MAXC + 1], list[MAXW][MAXC + 1];
   int n, i, j, k, count[MAXW]={0}, tmp;           ////// added count[] array initialized to 0
   setvbuf(stdout, NULL, _IONBF, 0);
   n = 0;
   printf("Enter text with EOD to mark end: ");
   while (1) {
       scanf("%s", word);
       if (0 == strcmp("EOD", word))
           break;
       // change first letter to lower-case if word is capitalized
       if ('A' <= word[0] && 'Z' >= word[0])
           word[0] += 'a' - 'A';
       // go through list of words
       for (i = 0; i <= n; i++) {
           // if at end of list (i==n) add the new word
           if (i == n) {
               strcpy(list[n], word);
               count[n]++;           ////// unique word added to list increments count by 1
               n++;
               break;
           }
           // compare word to word in list at position i
           k = strcmp(word, list[i]);
           // when k==0 word is not a new word
           if (k == 0) {
//               count[i]++;           ////// non-unique word increments count by 1
               break;
           }
           // when k < 0 we want to insert word into the list at position i
           if (k < 0) {
               // move rest of list to make room
               for (j = n; j > i; j--)
                   strcpy(list[j], list[j - 1]);
                   tmp = count[j-1]; count[j-1] = count[j]; count[j] = tmp;   ////// move count along with word
               // insert new word at position i
               strcpy(list[i], word);
               n++;
               break;
           }
           // note if k > 0 we just want to go onto the next position in the list
       }
   }

   printf(" Word list: ");
   for (i = 0; i < n; i++)
       printf("%s (%d) ", list[i], count[i]);
   return EXIT_SUCCESS;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote