C Linux Code to count number of times given word repeated in a file

/*C Linux Code for Counting the given word in given text file*/
#include<stdio.h>
#include<string.h>

void repeatCount();

int main()
{
repeatCount();
return 0;
}

void repeatCount()
{
FILE *fp;
fp=fopen("m1.txt","r");
char ch;
char str_rep[20];

char str_temp[20];
int count=0;
int wordcount=0;

printf("Enter word to look for repeat:\n");
scanf("%s",str_rep);

while((ch=getc(fp))!=EOF)
{
//printf("\ninside while\n");
if(ch==' ')
{
// printf("inside ch=\n");
str_temp[count]='\0';

if(strcmp(str_temp,str_rep)==0)
{
//printf("inside strcmp\n");
wordcount++;
count=0;
}
else
{
//printf("inside else of strcmp\n");
count=0;}
}//if(ch=' ') ends
else// else of ch=' '
{
str_temp[count++]=ch;
}

}//while endsi

printf("The word %s repeats %d times.\n",str_rep,wordcount);

fclose(fp);
fflush(stdin);
}//repeatCount ends

No comments:

Post a Comment