I currently work on a text file where it has a fixed number of words. And all I want is to count the occurrence of a word in a text file and output its density. I have 266 words inside a text file and I want to output the count and density of words and the word itself.
e.g. (sample.txt)
The quick brown fox jumps over the lazy brown dog.
Output:
**Count Density Word**
2 0.2% The
2 0.2% brown
OP's code:
#define DELIM " "
#include <stdio.h>
int main()
{
int c;
int count = 0;
FILE *file, *temp;
char line[200];
char *result, *result2;
file = fopen("sample.txt", "r");
temp = fopen("temp.txt", "w");
if (file)
{
while ((c = getc(file)) != EOF)
{
if (c == '.' || c == '(' || c == ')' || c == ',' || c == ':' || c == '-' || c == '’')
{
fputc(putchar(' '), temp);
continue;
} else
{
count = count + 1;
fputc(c, temp);
}
}
fclose(file);
fclose(temp);
temp = fopen("temp.txt", "r");
while (fgets(line,200,temp) != NULL)
{