GOC104 "Sub-string Count" - Editorial

PROBLEM LINK:

https://www.codechef.com/problems/GOC104
https://www.codechef.com/GMCD2016/problems/GOC104

Author: https://www.codechef.com/users/pprasadj
Tester: https://www.codechef.com/users/pprasadj
Editorialist: https://www.codechef.com/users/nileshdbit

DIFFICULTY:

EASY

PREREQUISITES:

Substring

PROBLEM:

You are given a task to read a text “M”. This text M can contain any kind of character from
alphabets, numbers to special characters. Your teacher tell you to look for a word say “N” in the text
“M”. The word “N” may contain alphabets, numbers or special characters. You are also having an
assignment to report the number of times the word “N” occurs in text “M”.

INPUT:
First line of the input is M value i.e. length of text. 1<= M <= 2000
Second line of the input is N value i.e. length of word. 1<= N <= 200 . Also N <=M

OUTPUT:
First line of the output is no. of time word to look for is repeated in text.

### AUTHOR'S AND TESTER'S SOLUTIONS:

#include <stdio.h>
#include <string.h>
int main(void)
{

char mainstring[2000];
char substring[200], *ret;
int i=0, count=0;
char *p, *q;
gets(mainstring);
fgets(substring, sizeof(substring), stdin);
substring[strlen(substring)-1]='\0';
ret=strstr(mainstring,substring);
p = mainstring;
q = substring;
//printf("%s",substring);
while ((p= strstr(p,q)) != NULL )
{
p += strlen(q);
count++;
}
//puts("\n");
printf("%d", count);
return 0;
}