Question

C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

C Programming Language on Linux - Word Frequency Program

Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following:

First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0.

Then, read the file (second pass) and for each word identified, search the linked list, and when found, increment the word frequency by 1.

Now, create a function called PrintList. This will print out each word and its respective word frequency count in the order of appearance.

Next, sort the linked list alphabetically (assuming that each word starts with a letter) and then invoke the PrintList function.

Finally, sort the linked list in decreasing order of word frequency (ignoring the alphabetic order for words with the same frequency) and then invoke the PrintList function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Steps to make this program:

  • First, make a file words.txt and save it in the same folder you are saving your C program file.
  • Then write in the file one word per line.
  • In the last line write END because while reading the file you are not reading the last line.

  • Then make a file WordFrequency.c and write the following code and then save.
  • Below is the fully tested code explained in comments:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*Declaring a node of the linked list*/
struct node
{
char word[100];
int freq;
struct node*next;
};
struct node *insert(struct node *head, char data[100])
{
int i;
struct node *tmp;
/*If there is no element in linked list then inserting data as first element*/
if(head==NULL)
{
tmp=(struct node*)malloc(sizeof(struct node));
for(i=0;i<100;i++)
tmp->word[i]=data[i];
tmp->freq=0;
head=tmp;
tmp->next=NULL;
return head;
}
/*Otherwise inserting data at the end of the linked list by traversing the whole linked list*/
struct node* p=head;
while(p->next!=NULL)
p=p->next;
tmp=(struct node*)malloc(sizeof(struct node));
for(i=0;i<100;i++)
tmp->word[i]=data[i];
tmp->freq=0;
tmp->next=NULL;
p->next=tmp;
return head;
}
void incrementfreq(struct node *head,char data[100])
{
struct node *p=head;
/*Traversing through the list*/
while(p->next!=NULL)
{
/*When word appears in linked list increasing its frequency and breaking from the loop*/
if(strcmp(p->word,data)==0)
{
p->freq+=1;
break;
}
p=p->next;
}
}
void printlist(struct node *head)
{
/*Printing the list by traversing through the list*/
struct node* p=head;
while(p!=NULL)
{
/*Not printing the words with 0 frequency*/
if(p->freq!=0)
{
printf("Word: %s",p->word);
printf("Frequency: %d ",p->freq);
}
p=p->next;
}
}
void sortalphabetic(struct node*head)
{
struct node *p,*q;
char data[100];
int tmp;
/*Selection sort by exchanging the data*/
for(p=head;p->next!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
/*If a word in left of linked list is not in alphabetic order with a word in right of linked list*/
if(strcmp(p->word,q->word)>0)
{
/*Swapping those two nodes*/
strcpy(data,p->word);
strcpy(p->word,q->word);
strcpy(q->word,data);
tmp=p->freq;
p->freq=q->freq;
q->freq=tmp;
}
}
}
}
void sortfrequency(struct node *head)
{
int tmp;
struct node *p,*q;
char data[100];
/*Selection sort by exchanging the data*/
for(p=head;p->next!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
/*If a word's freq in left of linked list is lesser than a word in right of linked list*/
if(p->freq<q->freq)
{
/*Swapping those two nodes*/
strcpy(data,p->word);
strcpy(p->word,q->word);
strcpy(q->word,data);
tmp=p->freq;
p->freq=q->freq;
q->freq=tmp;
}
}
}
}
int main()
{
FILE *fileptr;
char data[100];
/*Prompting user for file name*/
printf("Enter file name: ");
char filename[100];
scanf("%s",filename);
/*Opening the file*/
fileptr=fopen(filename,"r");
/*Declaring head pointer of the linked list*/
struct node *head=NULL;
/*Reading data from file line by line except last line and appending to the linked list*/
while(fgets(data,100,fileptr)!=NULL)
{
head=insert(head,data);
}
/*Closing the file*/
fclose(fileptr);
FILE *fileptr1;
/*Again opening the file*/
fileptr1=fopen(filename,"r");
/*Incrementing frequency of items in linked list by reading from the file line by line except the last line*/
while(fgets(data,100,fileptr1)!=NULL)
{
incrementfreq(head,data);
}
/*Closing the file*/
fclose(fileptr1);
/*Printing the file*/
printf("Printing the list with frequencies: ");
printlist(head);
/*Sorting the list by alphabetic order by selection sort of exchanging the data*/
sortalphabetic(head);
/*Printing the file*/
printf("Printing the list after sorting in alphabetic order: ");
printlist(head);
/*Sorting the list by decreasing order of frequency by selection sort of exchanging the data*/
sortfrequency(head);
/*Printing the file*/
printf("Printing the list after sorting in decreasing order of frequency ");
printlist(head);
return 0;
}

Sample output for the above input file words.txt


Add a comment
Know the answer?
Add Answer to:
C Programming Language on Linux - Word Frequency Program Please write a Program in C that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Program Word frequency analysis. [32] Write a program which can accept via stdin any number...

    C++ Program Word frequency analysis. [32] Write a program which can accept via stdin any number of words until the user types quit. Load the words into an array (use realloc to control the size). Then output the words in alphabetic order and give the number of times the word was used. Call this frequency.cpp.

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically,...

    Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically, the program will first determine the number of files to be processed. Then, the program will createmultiple threads where each thread is responsible for one file to count the number of words appeared in the file and report the number of time each word appears in a global linked-list....

  • Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

    Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt. Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code. File format: apple banana pear . . . Hint: Program will need to utilize double pointers. More Hints: 1. Assume no word is bigger that 50...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • C++ Program (Linux): Create a bubble sort that can read any list of number from a...

    C++ Program (Linux): Create a bubble sort that can read any list of number from a text file and print them out. Also print the number of comparison (how many time is had to compare number in order to sort).

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of...

    Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of signed doublewords, a parameter that indicates the length of the two arrays, and a parameter that indicates the maximum allowed difference (called diff) between any two matching elements. For each element x(i) in the first array, if the difference between it and the corresponding y(i) in the second array is less than or equal to diff, increment a count. At the end, return a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT