Write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called
Write a program to count the number of times the words in an input text file occur.
The WordCount Structure
Define a C++ struct called WordCount that contains the following data members:
An array of 31 characters named word
An integer named count
Functions
Write the following functions:
int main(intargc, char* argv[])
This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array elements that are in use.
If no input file name is specified as a program argument when the program is run, then argc will be equal to 1. If so, print an error message similar to the following and exit the program:
Usage: assign1 [file-name]
Call the function countWords() passing argv[1] as the file name parameter. Store the value returned by the function in numWords.
Call the function sortWords() to sort the array.
Print a header line as shown in the sample output, then call printWords() to print the words and their counts.
intcountWords(const char* fileName, WordCountwordArray[])
Parameters: 1) A C string that will not be changed by the function and that contains the name of an input file; 2) an array of WordCount objects.
Returns: The number of distinct words that the function stored in the array (i.e., the number of array elements filled with valid data).
This function should declare a file stream variable and open it for the file name passed in as the first parameter. If the file fails to open successfully, print an error message and exit the program.
Declare an integer numWords to keep track of the number of distinct words stored in the array of ofWordCount objects. This variable should be initialized to 0.
The function should then read words from the file as C strings using the >> operator until end-of-file is reached. For each word read, the function should do the following:
Call stripPunctuation() to strip any punctuation from the beginning and end of the word string. If the resulting string is empty (length 0), the remaining steps can be skipped.
Call stringToUpper() to convert the word string to uppercase.
Call searchForWord() to search for the word string in the array of WordCount objects.
If the index returned by the search is -1, this is a new word that must be added to the end of the array. Copy the word string into the word data member of the next empty array element (the one at index numWords), and set its count data member to 1. Then increment numWords.
Otherwise, this word has been found in the array. Increment the count data member for the WordCount object at the index returned by the search.
Once all words have been read from the file, the file should be closed and numWords should be returned.
voidstripPunctuation(char* s)
Parameters: 1) A C string that contains a word to be stripped of punctuation.
Returns: Nothing.
This function should remove any punctuation characters at the beginning and end of the C string s. For example:
The string "textile" should become textfile
The string (Wikipedia, should become Wikipedia
The string content. should become content
The string generic should remain generic
It is possible (although rare) for a string to contain nothing but punctuation characters. In that case, the result of executing this function should be an empty string.
There are a number of valid approaches to solving this problem.
You will need to be able to distinguish between punctuation characters and non-punctuation (or alphanumeric) characters; the C library functions isalnum() and ispunct() can help you do that.
Performing the required modifications to the string "in place" may be difficult, so feel free to use a local temporary character array to make your changes and then copy the final result back into s at the end of the function.
voidstringToUpper(char* s)
Parameters: 1) A C string that contains a word to be converted to uppercase.
Returns: Nothing.
This function should loop through the characters of the C string s and convert them to uppercase using the C library function toupper().
intsearchForWord(const char* word, constWordCountwordArray[], intnumWords)
Parameters: 1) A C string that will not be changed by this function and that contains a word to search for; 2) an array of WordCount objects to search that will not be changed by this function; 3) the number of elements in the array filled with valid data.
Returns: If the search was successful, returns the index of the array element that contains the word that was searched for, or -1 if the search fails.
This function should use the linear search algorithm to search for the C string word in wordArray.
voidsortWords(WordCountwordArray[], intnumWords)
Parameters: 1) An array of WordCount objects to sort; 2) the number of elements in the array filled with valid data.
Returns: Nothing.
This function should sort the array of WordCount objects in ascending order by account number using the selection sort algorithm.
The sort code linked to above sorts an array of integers called numbers of size size. You will need to make a number of changes to that code to make it work in this program:
Change the parameters for the function to those described above.
In the function body, change the data type of temp to WordCount. This temporary storage will be used to swap elements of the array of WordCount objects.
In the function body, change any occurrence of numbers to the name of your array of WordCount objects and size to numWords (or whatever you called the variable that tracks the number of array elements filled with valid data.
The comparison of numbers[j] and numbers[min] will need to use the C string library function strcmp() to perform the comparison. The final version of the if condition should look something like this:
if (strcmp(wordArray[j].word, wordArray[min].word) < 0)
...
It is legal to assign one WordCount object to another; you don't need to write code to copy individual data members.
voidprintWords(constWordCountwordArray[], intnumWords)
Parameters: 1) An array of WordCount objects to print that will not be changed by this function; 2) the number of elements in the array filled with valid data.
Returns: Nothing.
This function should loop through the array and print each word and its corresponding count, neatly formatted into columns similar to the sample output. It should also print the number of words in the file (which is equal to the sum of the counts) and the number of distinct words (equal to numWords).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
assign1cpp include include include using namespace std structure defintion struct...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Document Format ( 2 attachments)
609b0a25de7fd_31260.pdf
180 KBs PDF File
609b0a25de7fd_31260.docx
120 KBs Word File
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started