Question: #include int totalWordCount ( char * filename, int max _ word _ length ) { / / If the file cannot be found, return -

#include int totalWordCount(char * filename, int max_word_length){
// If the file cannot be found, return -1
fptr = fopen(filename,"r");
if (fptr == NULL)
{
return -1;
}
// Dynamically allocate a string of length max_word_length
// Read words to that string, using fscanf, and count the words
// Hint: fscanf(file_ptr,"%39s", my_string) will retrieve everything up to the next whitespace (up to 39 characters), add a
// Close the file and free the string
// Return the word count
}
// Inputs:
// char * filename The name of the input file
// int max_word_length The longest any word is allowed to be
// int total_word_count The total number of words in the file
void eachWordCount(char * filename, int max_word_length, int total_word_count)
{
// Open the file
// If fopen fails, return
// Dynamically allocate space for your table of words, both words and ints to act as word counts
// Hint: you can use pointer arithmetic or double pointers to navigate your table of words
// Hint: you may want a variable to track how many rows of your table contain real data
// Read in words from the file
// convert them to lowercase using the
// if the word does not appear in the table, add it and give it a count of 1
// if the word does appear in the table, increment its count
// Hint: you can use "strcmp" to compare if two strings are identical
// Close the file
// Print out words and their counts
// Free all dynamically allocated memory
} This assignment counts the number of words in a file, allocates space for a list of these words, then goes back
through the file to identify all the distinct words and count how many times each distinct word occurs.
For example, given a file containing the phrase:
Fool me once, shame on you. Fool me twice, shame on me.
The program would count the words. There are twelve words. Then it would make space for a table of twelve
words and numbers. Then it would go through the file and look at each word:
"Fool" would be added to the table with count 1.
"Me" would be added to the table with count 1.
"Once" would be added to the table with count 1.
"Shame" would be added to the table with count 1.
"On" would be added to the table with count 1.
"You" would be added to the table with count 1.
"Fool", already being in the table, would get count 2.
*"Me", already being in the table, would get count 2.
"Twice" would be added to the table with count 1.
"Shame", already being in the table, would get count 2.
"On", already being in the table, would get count 2.
"Me", already being in the table with count 2, would get count 3.
The program would then print out: fool 2 me 3 once 1 shame 2 on 2 you 1 twice 1
You may notice that the phrase input has punctuation and capital letters, while the table of output does not. A
function is provided to convert all words (that is, blocks of characters separate by spaces) into their dictionary
form (lowercase letters only).
This output will be sorted (that is to say, the lines will be alphabetized) before being compared with a correct
answer, so it's okay if your program prints things in a different order. Check Memory Leak
Your program must not crash and must not leak memory. The leaked bytes will accumulate across all test cases in
each assignment. If your program leaks memory, it is very likely that you will receive zero. Please use valgrind to
check memory errors. A program that leaks memory is like an airplane that leaks fuel. Neither can be accepted.
WHAT TO SUBMIT
Submit the following files to gradescope as a zip file: 'main.c', 'filestr.c', 'filestr.h'.
Efficiency
It may seem like your program takes up a lot of empty space with its word table, and searches inefficiently through
that table to see if a word has already been added. In ECE 368, you'll learn about more advanced data structures
and algorithms that will help conserve space and time.
#include int totalWordCount ( char * filename,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!