Due: December 4, 2015
Points:100
You must put your program in a file called “words.c”. It must also be a C program (not RTF, for example). |
You are to write a program that reads one or more files, sorts the words in the files, and then prints each word prefixed by the number of times it appears in the files. For this assignment, a “word” is a maximal sequence of alphanumeric characters. So, for example, “hello”, ”abcdefgh”, “1245”, and “good4you” are all words. Any non-alphanumeric terminates a word. You can assume no word has more than 99 characters.
For example, if given the Declaration of Independence, your program should print the following:
1 1776because all the above words appear once except for “Acts”, “America”, and “Armies”, each of which appears twice, and “Assent”, which appears four times.
1 4
1 A
1 Absolved
2 Acts
1 Administration
1 Allegiance
1 Alliances
2 America
1 And
1 Annihilation
1 Appropriations
1 Arbitrary
2 Armies
1 Arms
1 Assembled
4 Assent
Because you will not know how many different words the input files contain, you must use a linked list to hold the words. As you encounter each word in the text, you should add it to the linked list. It’s probably easiest if you do the insertion sort like the example in linked.c, but you can build the list and sort it afterwards. The structure for a word should also contain a field to hold a count of the number of times the word appears in the input files.
When you sort the words, sort them in ASCII order. You can use the function
Your program is to take 0 or more file names as arguments. If there are no files named, your program should exit with no output. If there are no errors (not naming any files is not an error), exit with exit code 0.
If a file that is named cannot be read, use the function perror to print the error message. If fname is the name of the file that cannot be opened, call the function like this:
If you are unable to allocate memory for a word, please print the error message:
The format for printing a word is
You can also obtain a PDF version of this. | Version of November 26, 2015 at 8:53PM |