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

I am trying to make a code in C that reads all of the words typed, makes them lo

ID: 3864107 • Letter: I

Question

I am trying to make a code in C that reads all of the words typed, makes them lower case, and counts how many times the word was typed in. I have a code but it is not counting the amount of times the word was typed in properly.

#include
#include
#include
#define MAXW 500
#define MAXC 50

int main(void) {
   char word[MAXC + 1], list[MAXW][MAXC + 1];
   int n, i, j, k, count[100000]={1};
   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;
       if ('A' <= word[0] && 'Z' >= word[0])
           word[0] += 'a' - 'A';
       for (i = 0; i <= n; i++) {
           if (i == n) {
               strcpy(list[n], word);
               n++;
               break;
}
           k = strcmp(word, list[i]);
           if (k == 0) {
               count[i]++;
               break;
           }
           if (k < 0) {
               for (j = n; j > i; j--)
                   strcpy(list[j], list[j - 1]);
               strcpy(list[i], word);
               n++;
               break;
           }
       }
   }

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

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[100000]={0};
   setvbuf(stdout, NULL, _IONBF, 0);
   n = 0;
   printf("Enter text with EOD to mark end: ");

   while (1) {
       scanf("%s", word);
     
       /* Checking for EOD
        */
       if (0 == strcmp("EOD", word))
           break;
         
       /* Converting the string to lower case
        */  
       for (i=0; word[i]; i++) {
            word[i] = tolower(word[i]);
       }
     
       for (i = 0; i <= n; i++) {
           /*
            * If the word doesn't exists and it is last in alphabetical order,
            * add it to the end. set the count to 1
            *
            */
           if (i == n) {
               strcpy(list[n], word);
               n++;
               count[i] = 1;
               break;
           }
         
           /* If the word already exists, increment the count
            */
           k = strcmp(word, list[i]);
           if (k == 0) {
               count[i]++;
               break;
           }
         
           /*
            * Add the word into the appropriate position in the list
            * Also adjust count array values
            */
           if (k < 0) {
                for (j = n; j > i; j--)
                   strcpy(list[j], list[j - 1]);
                strcpy(list[i], word);
                for (j = n; j > i; j--)
                   count[j] = count[j - 1];
                count[j] = 1;
                n++;
                break;
           }
       }
   }

   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