You are to write a program that reads one or more files, sorts the words in the
ID: 3766871 • Letter: Y
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(fname);
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
(the “ ” 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
(“” is a single space and, again, the “ ” 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.
Explanation / Answer
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int occurences[1000]={1};
struct Word
{
char word[100];
struct Word *nextWord;
}*headWord;
void insert(char []);
void display();
int main(int argc, char *argv[])
{
FILE *fp;
char tempWord[100];
struct Word *headWord=NULL;
int i,j,flag,t;
printf("%d-%s-%s ",argc,argv[0],argv[1]);
for(i=1;i<=argc-1;i++)
{
flag=0;
fp=fopen(argv[i],"r");
if(fp==NULL)
perror(argv[i]);
while(!feof(fp))
{
fscanf(fp,"%s",&tempWord);
if(strlen(tempWord)<100)
{
//checking for non alpha numeric string
for(j=0;j<strlen(tempWord);j++)
{
if(!isalnum(tempWord[j]))
{
flag=1;
}
}
if(flag!=1)
{
t=exist(tempWord);
if(t!=-1)
{
occurences[t]++;
}
else
{
insert(tempWord);
}
flag=0;
}
}
}
}
display();
return 0;
}
void insert(char tempword[])
{
struct Word *temp1,*temp2;
temp1= (struct Word*)malloc(sizeof(struct Word));
temp1->nextWord=NULL;
strcpy(temp1->word,tempword);
if(headWord==NULL)
{
headWord=(struct Word*)malloc(sizeof(struct Word));
headWord=temp1;
}
else
{
temp2=headWord;
while(temp2->nextWord!=NULL)
{
temp2=temp2->nextWord;
}
temp2->nextWord=temp1;
}
}
void display()
{
struct Word *temp1,*temp2;
int i=0;
temp2=headWord;
while(temp2->nextWord!=NULL)
{
printf("%d-%s ",occurences[i],temp2->word);
temp2=temp2->nextWord;
i++;
}
}
int exist(char words[])
{
struct Word *temp1,*temp2;
int i=0;
temp2=headWord;
while(temp2->nextWord!=NULL)
{
if(strcmp(words,temp2->word)==0)
return i;
temp2=temp2->nextWord;
i++;
}
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.