I want to write a program to count the number of occurances of a substring in a main string. am having trouble with this please i need help. my code is below, you can show be a better way to write it and tell me were i went wrong. am new to this
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], rstr[100];
int i =0,*k;
printf("Enter a string \n");
gets(str);
printf("Enter a sub string \n");
gets(rstr);
k = strstr(str,rstr);
while (k!= NULL){
i++;
k = strstr(k+1,rstr);
}
printf("it occurs %d times \n", i);
return 0;
}