In C Programming: Write a program to find the frequency of words in a file. You
ID: 652208 • Letter: I
Question
In C Programming:
Write a program to find the frequency of words in a file. You need to use dynamic memory allocation for this assignment. Use a dynamically allocated array of pointers to store the words and frequencies. You can use calloc() to allocate the array and realloc() to increase the size of the array to insert more elements. 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.txt is the input file which contains one word per line, frequencies.trt 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 apple Output file for above input is given below 3 apple 2 orange 1 banana Don't forget to deallocate all the space allocated using malloc() and calloc() using freeQ 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
#include <stdio.h>
#include <stdlib.h>
struct wordfreq
{
int count;
char *word;
};
typedef struct wordfreq wordfreq;
int main(int argc, char *argv[])
wordfreq **wordarray;
int i;