Write a program in C language not C++ with output that: Open an existing text fi
ID: 3706291 • Letter: W
Question
Write a program in C language not C++ with output 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 100 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 memory that it has allocated. Some functions you will need: fopen, fgets, strtok, toupper, qsort, puts, fclose, malloc, realloc, free. Hint: You will need an array of structures struct word (char *word; unsigned count;); You can either start with an empty array and expand it by one cell every time you find a new word, or allocate a good-sized array, keep track of its occupancy, and expand the array when it gets full. The latter approach is preferred.Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXWORDS 100
#define MAXSTRING 100
/* hold word frequency information */
typedef struct _word {
char s[MAXSTRING];/* the word */
int count;/* number of times the word occurs */
} word;
/*
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*/
strcpy (words[*n].s, s);
/* this word has occured once up to now, so count = 1 */
words[*n].count = 1;
/* one more word */
(*n)++;
}
/* sort words by descending order of count */
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 uppercase */
void make_uppercase (char *s) {
int i;
for (i=0; s[i]; i++) s[i] = toupper(s[i]);
}
/*
taking input from user . to stop the input and view the result press ctrl + Z
(^Z = ctrl + z)
example 1 : qwQW asAS^Z
result ASAS 1
QWQW 1
example 2 : qw QW
as AS^z
result AS 2
QW 2
*/
void main () {
word words[MAXWORDS];
char s[1000];
int i, n, m;
n = 0;
clrscr();
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_uppercase (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 100 words in total, just print up the the first n words */
if (n < 100)
m = n;
else
m = 100;
/* print the words with their frequencies */
for (i=0; i<m; i++)
printf ("%s %d ", words[i].s, words[i].count);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.