The question goes like this:
You are given a file in which each line contains a list of space separated words.
Write a program to find out:
1. List of words that are present in all the lines (words common to all lines)
2. List of unique words that are present in the file
3. The length of the line and file can be of any arbitrary size and your program can *not* hardcode either one of them. To reiterate, your program cannot allocate an array of arbitrary size or malloc an arbitrarily huge size in the beginning. It should dynamically allocate memory as and when required.
Following are the requirements for the output of your program:
The output of your program should contain two lines.
• First line will have a space separated list of the words that are present in all the lines.
• The next line will have a space separated list of all words occurring in the file. If the same word appears more than once in two different lines, your program should print it only once.
The order of the words printed in the output should be the same as the order in which the words occur in the input file.
For example if the input-file by name "a.txt" contains:
good cat rat mat bat sat fat great bad
bad good great sat fat fun
cat bat fat mat sat good great fun find
The output expected is:
common words: good sat fat great
unique words: good cat rat mat bat sat fat great bad fun find
Notice that the order in which the words are printed by your program is important. The output
common words: good sat fat great
is NOT the same as
common words: sat fat good great
because that is not the order in which these words appear in the file.
Your program will be invoked as
<your_program> a.txt
That is the file name containing lines with space separated words will be passed as a command line argument to your program.
Your program should strictly adhere to the format of output given in the problem statement for being considered for evaluation.
With respect to the input file you can assume that:
• There are no blank lines
• There is at least one word in each line
• A word never repeats itself in the same line
I have written the following code:
#include<stdio.h>
#define ch_limit 500
struct node {
char word[ch_limit];
int count;
struct node *next;
};
typedef struct node *list;
int count_lines(FILE *fp)
{
return;
}
list populate(char *word,list first)
{
list temp,new=first;
int value=0;
temp=(list)malloc(sizeof(list));
strcpy(temp->word,word);
temp->count=0;
temp->next=NULL;
if(first==NULL){
first=temp;
return first;
}
while(new->next!=NULL){
if(strcmp(new->word,word)==NULL){
value=new->count;
value+=1;
new->count=value;
free(temp);
return(first);
}else{
printf("I am inside");
new=new->next;
}
}
if(strcmp(new->word,word)==NULL){
value=new->count;
value+=1;
new->count=value;
return(first);
}
new->next=temp;
return(first);
}
list read_word(FILE *input_file)
{
char word[ch_limit];
int ch;
int i,count=0;
list first=NULL;
while((ch=fgetc(input_file)) !=EOF){
for(i=0;(ch!=' ') || (ch!='\n');i++){
word[i]=ch;
ch=fgetc(input_file);
if (ch == EOF)
break;
}
word[i]='\0';
first=populate(word,first);
}
return(first);
}
int display(list first,int line_nos)
{
list unique,common;
common=unique=first;
if(unique==NULL){
printf("The list of unique words is empty\n");
return;
}
printf("Unique List: ");
while(unique){
puts(unique->word);
printf(" ");
unique=unique->next;
}
printf("\n");
if(common==NULL){
printf("The list of common words is empty\n");
return;
}
printf("Common List: ");
while(common){
if(common->count!=line_nos){
puts(common->word);
printf(" ");
}
common=common->next;
}
return;
}
int main(int argc, char *argv[])
{
FILE *input_file;
int line_nos;
list first=NULL;
if(argc!=2) {
printf("Usage: %s filename\n",argv[0]);
exit(0);
}
if((input_file=fopen(argv[1],"r"))==NULL){
printf("Error in opening the file\n");
exit(0);
}
line_nos=count_lines(input_file);
first=read_word(input_file);
display(first,4);
close(input_file);
}
However, the output obtained for the above code is:
# ./a.out txt.txt
List: go so go mo bo
go inter yak lo
jai go kim yoka
my go bat cat
Common List: go so go mo bo
go inter yak lo
jai go kim yoka
my go bat cat
Please help me in debugging whats wrong in the code :(((