In C Programming: (100 pts) Write a program to find the frequency of words in a
ID: 651617 • Letter: I
Question
In C Programming:
(100 pts) 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 army 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.txt 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 celiac() using free() function. Rim your program under valgrind as shown below to verify that you have no memory leaks. eik05 > valgrind assign6 words.txt frequencies.txt Somewhere in the output it should say All heap blocks are freed.Explanation / Answer
#include <stdio.h>
#include <string.h>
int total= 0;
struct wordfreq
{
int count;
char word[];
};
* removing punctuations and all upper case letters are turned into lower case */
void removedelimiters (char sentence[])
{
int k;
for (k = 0; k< strlen(sentence); k++)
{
if ( ((int)sentence[k] >=33 && (int)sentence[k] <=47) ||
((int)sentence[k] >=58 && (int)sentence[k] <=63) ) // all the punctuation
{
if (k == strlen(sentence) -1)
sentence[k] = '';
else
sentence[k] = ' ';
}
if ((int)sentence[k] >=65 && (int)sentence[k] <=90) //if upper case
(int)sentence[k] += 32; //make into lower case
}
return;
}
/* Takes in a word, and compares it with existing words. Returns 0 if it has not previously been saved */
int compare (char fake[], struct wordfreq w[])
{
int k=0;
extern int total;
for (k=0; k<total; k++)
if (strcmp(fake, w[k].word) == 0)
return k;
return 0;
}
void process (char sentence[], struct wordfreq w[])
{
char fake[30];
extern int total;
int x, y;
removedelimiters(sentence);
while(y!=0)
{
if (strstr(sentence, " ") == NULL) //if word is at end of sentence
y= 0;
else
*(strstr(sentence, " ")) = ''; //cut off point where first space
//is i.e. a word
strcpy(fake, sentence); //copy to fake variable
x = compare (fake, w);
if (x == 0)
{
strcpy(w[total].word, fake); //assign to new variable
w[total].count = 1; //set initial countuency
total++;
}
else //word exists already
w[x].count++;
if (*(strchr(sentence,'')+1) == ' ')
sentence = strchr(sentence,'')+2;
else
sentence = strchr(sentence, '')+1;
}
return;
}
int main (void)
{
FILE *infile, *outputFile;
struct wordfreq *s = malloc(sizeof(struct wordfreq) + 500);
char sentence[100];
struct wordfreq w[500]; //assuming that there are no more than 500 different words
extern int total;
int k=0;
infile = fopen ("doc.txt", "r");
outputFile = fopen ("count.txt", "w");
while(fscanf(infile, "%c", &sentence[0]) == 1)
{
k=1;
do{
fscanf (infile, "%c", &sentence[k]);
k++;
} while (sentence[k-1] != ' ');
sentence[k-1] = '';
printf("%s ", sentence);
process (sentence, w);
}
for (k=0; k<total; k++)
fprintf(outputFile, "%s %i ", w[k].word, w[k].count);
free(s);
return 0;
}