Question
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 1776
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 all the above words appear once except for "Acts", "America", and "Armies", each of which appears twice, and "Assent", which appears four times. 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.

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: perror (name) ; This function will print the name of the file followed by a colon and an error message stating why the file could not be opened. This is written on the standard error (not the standard output). Then continue until all named files have been processed, and exit with exit code 1.

If you are unable to allocate memory for a word, please print the error message: %s, file %s, line %d: ran out of memory\n (the "\n" is a newline) with the first "%s" being replaced with the word, the second with the name of the file being processed when the error occurs, and the "%d" being replaced by the line number being processed when the error occurs. As above, exit with exit code 1. The format for printing a word is %5d_%s\n (again, the "\n" is a newline) where the "%d" is replaced by the count (the number of times the word appears in the input file(s) and "%s" is replaced by the word.
Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

*
* LINKED LIST SORTER
*
* This program reads in wordbers and sorts them in increasing
* worderical order. The data structure used is a linked list;
* each element looks like this:
* +--------------+
*      | data field | <--- holds the integer that you read in
* +--------------+
*      | next field | <--- holds pointer to next element in
* +--------------+       linked list (NULL if nothing
*                       follows it)
*
* The pointer variable "head" contains a pointer to the first
* element in the linked list (NULL if there are no elements
* in the linked list)
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>

/*
* structure for the list
*/
struct word {
    char * data; /* data field (wordber to be sorted) */
    struct word *next;    /* points to next element in list */
                         /* (NULL pointer if no next element) */
    int counter;
};

typedef struct word word;
This is only a preview of the solution.
Please use the purchase button to see the entire solution.
By purchasing this solution you'll be able to access the following files:
Solution.c
Purchase Solution
$30.00
Google Pay
Amazon
Paypal
Mastercard
Visacard
Discover
Amex
View Available Computer Science Tutors 645 tutors matched
Ionut
(ionut)
Master of Computer Science
Hi! MSc Applied Informatics & Computer Science Engineer. Practical experience in many CS & IT branches.Research work & homework
5/5 (6,808+ sessions)
1 hour avg response
$15-$50 hourly rate
Pranay
(math1983)
Doctor of Philosophy (PhD)
Ph.D. in mathematics and working as an Assistant Professor in University. I can provide help in mathematics, statistics and allied areas.
4.6/5 (6,702+ sessions)
1 hour avg response
$40-$50 hourly rate
Leo
(Leo)
Doctor of Philosophy (PhD)
Hi! I have been a professor in New York and taught in a math department and in an applied math department.
4.9/5 (6,469+ sessions)
2 hours avg response

Similar Homework Solutions