Write a program in C89 that takes in a TEXT FILE as input in
Write a program in C89 that takes in a TEXT FILE as input in via a command line argument. Output a bag of words of the words in the text file. Output this to bagofwords.txt. Look here for an understanding: https://en.wikipedia.org/wiki/Bag-of-words_model
Design the program to include encapsulating functionality and logical organization of functionality.
Bag of Words Output File
./a.out sometextfile.txt
John: 1
likes: 2
to: 3
watch: 4
movies: 5
also: 6
football: 7
games: 8
Mary: 9
too: 10
Solution
comments added
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
//variables declared
int number_of_words=0;
FILE *fw,*br;
char filename[20],character;
printf(\"PLease enter file name\");
gets(filename); //reading filename
br=fopen(filename,\"r\");
if(br==NULL)
{
printf(\"\ File cannot be opened\");//when file not found
exit(0);
}
character=fgetc(br);
while(character!=EOF)//iterating text to get chars,words,lines
{
if(character==\' \');
number_of_words++;
character=fgetc(br);
}
int i, j;
char myArray[128][128],duplicateArray[128][128];;
char eachline[128]; //required arrays declaring
for(i=0; i<128; i++)
for(j=0; j<128; j++)
myArray[i][j] = \'\\0\';
for(i=0; i<128; i++)
eachline[i] = \'\\0\';
if ( br != NULL )
{
i=0;
while ( fgets ( eachline, sizeof eachline, br ) != NULL ) //reading each line
{
strcpy(myArray[i], eachline);//copying to array
i++;
}
}
int frequency=0,k=0,c;
//comparing all words
for (i = 0;i <= number_of_words;i++)
{
for (j = 0;j <= number_of_words;j++)
{
if (i == j)
{
strcpy(duplicateArray[k], myArray[i]); //copying the array into duplicate array for comparision purpose
k++;
frequency++;
break;
}
else
{
if (strcmp(duplicateArray[j], myArray[i]) != 0)//matching the required word with all words
continue;
else
break;
}
}
}
for (i = 0;i < frequency;i++)
{
for (j = 0;j <= number_of_words;j++)
{
if (strcmp(duplicateArray[i], myArray[j]) == 0)
c++;
}
//printing final results
printf(\"%s -> %d times found \ \", duplicateArray[i], c); // final data printing
c = 0;
}
return 0;
}