The below code always returns number of matching sub strings as zero.There are no errors in the code and i am not sure where have i gone wrong logically.
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,len ,k ,count, num ;
char str[100],sub[100],comp[100] ;
// sub is the sub string .
printf("Please enter the string") ;
gets(str) ;
printf("Enter the substring to be searched for") ;
gets(sub) ;
len=strlen(sub) ;
for ( i=0 ; i < strlen(str) - len ; i++ )
//Goes till length of string - length of sub string so that all characters can be compared.
{
num = i + len ;
for ( j=i,k=0 ; j<num ; j++, k++ )
//Loop to store each sub string in an array comp.
{
comp[k]=str[j] ;
}
if ( strcmp(comp,sub) == 0 )
{ count++ ; }
}
printf("no of occurances is:%d",count) ;
return 0 ;
}