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

Please use C. Add notes to explain what is happening! Thank you! Write a program

ID: 3816142 • Letter: P

Question

Please use C. Add notes to explain what is happening! Thank you!

Write a program to find the frequency of words in a file. You need to use dynamic memory allocation for this assignment. Use array of pointers to store the words and frequencies. Set array size to 1000 and initialize all the pointers to NULL. Structure declaration to store words and frequencies is as follows struct wordfreq {int count; char *word;}; When you see a word for the first time, insert into the array with count 1. If the word read from file is already in the array, increase its count. In this structure, you need to dynamically allocate the space for each word using malloc(). Use argc and argv for input file and output file. Sample execution of the program is given below words. tart is the input file which contains one word per line. frequencies tat is the file to be generated by your program. It contains frequencies and words, one word and its frequency per line. Elk05> assign6 words .txt frequencies.txt Sample input file is given below apple orange apple banana orange orange Output file for above input is given below 2 apple 3 orange 1 banana Don't forget to deallocate all the space allocated using malloc() and calloc() using free() function. Run your program under valgrind as shown below to verify that you have no memory leaks. Elk05> valgrind assign6 words.txt frequencies.txt Somewhere in the output it should say All heap blocks are freed.

Explanation / Answer

PFB C Program to calculate the frequency of words in txt file:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
   if (argc == 1) {
   /* To print the error*/
   printf("The input file name has not been provided ");
   }
   else if (argc == 2) {
   FILE *f = fopen(argv[1], "rb");
   fseek(f, 0, SEEK_END);
   long fsize = ftell(f);
   fseek(f, 0, SEEK_SET);

   char *str = malloc(fsize + 1);
   fread(str, fsize, 1, f);
   fclose(f);

   str[fsize] = 0;
   int count = 0, c = 0, i, j = 0, k, space = 0;
   char p[1000][512], str1[512], ptr1[1000][512];
   /* Define Pointer */
   char *ptr;
   for (i = 0;i<strlen(str);i++)
   {
   if ((str[i] == ' ')||(str[i] == ',')||(str[i] == '.'))
   {
   space++;
   }
   }
   for (i = 0, j = 0, k = 0;j < strlen(str);j++)
   {
   if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
   {
   p[i][k] = '';
   i++;
   k = 0;
   }
   else
   p[i][k++] = str[j];
   }
   k = 0;
   for (i = 0;i <= space;i++)
   {
   for (j = 0;j <= space;j++)
   {
   if (i == j)
   {
   strcpy(ptr1[k], p[i]);
   k++;
   count++;
   break;
   }
   else
   {
       /*Used strcasecmp to neglect the case i.e. APPLE/apple will be the same*/
   if (strcasecmp(ptr1[j], p[i]) != 0)
   continue;
   else
   break;
   }
   }
   }
   for (i = 0;i < count;i++)
   {
   for (j = 0;j <= space;j++)
   {
   if (strcasecmp(ptr1[i], p[j]) == 0)
   /*increasing the count */
   c++;
   }
   /*To Print the out put */
   printf("%s %d ", ptr1[i], c);
   c = 0;
   }
   }
   return 0;

}