I'm trying to write a program that gets a string, and counts how many times that string is found in a specific file.
The file is currently: hello my name hello is oria I like hello to program
And the word I'm counting is hello.
this is my code
int num_of_words(FILE* stream,char* str)
{
int count=0,i=0,length;
char c;
rewind(stream);
length=strlen(str);
do
{
c=fgetc(stream);
while(c==*(str+i))
{
c=fgetc(stream);
i++;
if(i==length)
{
count++;
i=0;
}
}
i=0;
}while(c!=EOF);
return count;
}
The idea is that there is a certain index called i, and it advances only if there is a match between letters. if i reached the length of the string, then it means we found all the letters in succession, and i raise count by one.
For some reason, it always returns zero.