Write a C program that receives as input a text file and outputs the top 10 most
ID: 653478 • Letter: W
Question
Write a C program that receives as input a text file and outputs the top 10 most frequent sequences of 2 consecutive words starting with the word "he". For example, for the text below:
"He woke up this morning feeling energetic. Then he told his wife he was going to run at the park because he was feeling a little out of shape. Not knowing that today was his wife's birthday, he left the house without hesitating. To worsen the situation, he told his wife that he would not take long but he ended up having lunch out with friends, fact that really bothered his wife."
The output would be something like:
he told 2
he was 2
he woke 1
he left 1
he would 1
he ended 1
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void main()
{
char fname[50];
int i,k;
char* wordarr[20];
char word[20];
char str[20];
FILE *in;
int count=0;
printf("Enter the file name with extension:");
scanf("%s",fname);
in=fopen(fname,"r");
if(in==NULL)
{
printf("error in reading file / file missing");
exit(-1);
}
printf("Enter the desired word in file");
scanf("%s",word);
k=0; i=0;
while(!feof(in))
{
fgets(str,20,in);
printf("%s",str);
if(strcmp(str,word)==0)
{
fgets(str,20,in);
for(j=0;j<i;j++)
{
if(strcmp(str,wordarr[j])==0)
count[k++]+=1;
}
strcpy(wordarr[++i],str);
}
}
for(l=0;l<=i;l++)
{
printf("%s %s occurs %d no.of times",word,wordarr[l],count[l]);
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.