EDIT:
Question:
Write a program to find the number of times that a given word (i.e. a short string) occurs in a sentence (i.e. a long string!). Read data from standard input. The first line is a single word, which is followed by general text on the second line.
Sample Input:
the
the cat sat on the mat
Sample Output:
2
I tried using scanf before but it fails to read the entire sentence and just checks the first word and returns 1 as the answer instead of 2
The code is:
#include <stdio.h>
#include <string.h>
int main()
{
char s[200000], c[20], v = ' ';
int i = 0, j, f, n = 0;
gets(c);
gets(s);
while (i < strlen(s))
{
j = 0;
f = 0;
while (j < strlen(c))
{
if (s[i++] != c[j++])
{
f = 1;
i--;
break;
}
}
if ((f == 0) && (i == strlen(s) || s[i] == ' ') && (v == ' '))
{
n++;
v = s[i++];
}
printf("%d\n", n);
return 0;
}
}