Write a program in C language with output examples that: Open an existing text f
ID: 3705447 • Letter: W
Question
Write a program in C language with output examples that: Open an existing text file (with line breaks; each line is shorter than 150 characters). Read the file, Count the frequency of each word in the file, and Print the 75 most frequently used words to stdout in the decreasing order of their frequency. The length of the file is not limited. A word is defined as a sequence of non-blank characters (punctuation ok), with all letters converted to upper case. A frequency of a word shall be defined as the number of instances of the word in the text. The name of the text file shall be passed as the first command line parameter. The program shall deallocate (free) all memorv that it has allocated.Explanation / Answer
Note : Please find the c program for your requirement......Copy and run in the terminal......If any doubts or queries kuda ping in comment box.....Happy Coding!!
////////////////////////////WordFrequency.c////////////////
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXWORDS 10000
#define MAXSTRING 100
/* structure holding word frequency information */
typedef struct _word {
char s[MAXSTRING]; /* the word */
int count; /* number of times word occurs */
} word;
/*
* This function inserts a word into the list of words. If the word is
* not yet in the list, a new slot is used with the count set to 1.
* Otherwise, the count is simply incremented.
*/
void insert_word (word *words, int *n, char *s) {
int i;
/* linear search for the word */
for (i=0; i<*n; i++) if (strcmp (s, words[i].s) == 0) {
/* found it? increment and return. */
words[i].count++;
return;
}
/* error conditions... */
if (strlen (s) >= MAXSTRING) {
fprintf (stderr, "word too long! ");
exit (1);
}
if (*n >= MAXWORDS) {
fprintf (stderr, "too many words! ");
exit (1);
}
/* copy the word into the structure at the first available slot,
* i.e., *n
*/
strcpy (words[*n].s, s);
/* this word has occured once up to now, so count = 1 */
words[*n].count = 1;
/* one more word */
(*n)++;
}
/* comparison function for quicksort. this lets quicksort sort words
* by descending order of count, i.e., from most to least frequent
*/
int wordcmp (word *a, word *b) {
if (a->count < b->count) return +1;
if (a->count > b->count) return -1;
return 0;
}
/* return 1 if c is alphabetic (a..z or A..Z), 0 otherwise */
int is_alpha (char c) {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') return 1;
return 0;
}
/* remove the i'th character from the string s */
void remove_char (char *s, int i) {
while (s[i]) {
i++;
s[i-1] = s[i];
}
s[i] = 0;
}
/* remove non-alphabetic characters from the string s */
void remove_non_alpha (char *s) {
int i;
for (i=0; s[i]; i++) if (!is_alpha (s[i])) remove_char (s, i);
}
/* make all the letters in s lowercase */
void make_lowercase (char *s) {
int i;
for (i=0; s[i]; i++) s[i] = tolower (s[i]);
}
/* main program */
int main () {
word words[MAXWORDS];
char s[1000];
int i, n, m;
n = 0;
/* read all the words in the file... */
while (!feof (stdin)) {
scanf ("%s", s);
/* only insert the word if it's not punctuation */
if (is_alpha (s[0])) {
/* get rid of non-letters */
remove_non_alpha (s);
/* make all letters lowercase */
make_lowercase (s);
/* put this word in the list */
insert_word (words, &n, s);
}
}
/* sort the list of words by descending frequency */
qsort((void *) words, n, sizeof (word),
(int (*) (const void *, const void *)) wordcmp);
/* if fewer than 20 words in total, just print up the the
* first n words
*/
if (n < 75)
m = n;
else
m = 75;
/* print the words with their frequencies */
for (i=0; i<m; i++)
printf ("%s %d ", words[i].s, words[i].count);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.