0

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 ;
}  
dbush
  • 205,898
  • 23
  • 218
  • 273

2 Answers2

2

As mentioned in the comments, when constructing comp, you're not adding a terminating null byte at the end. Because the rest of comp is not initialized, you invoke undefined behavior when calling strcmp.

Add the null byte at the end of the inner for loop will fix the problem:

     for ( j=i,k=0 ; j<num ; j++, k++ )
     //Loop to store each sub string in an array comp.
       {
         comp[k]=str[j] ;
       }
     comp[k] = '\0';

Actually, rather than creating a separate substring, just use strncmp, which compares up to a certain number of characters:

for ( i=0 ; i < strlen(str) - len ; i++ ) 
//Goes till length of string - length of sub string so that all characters can be compared.
  {  
     if ( strncmp(&str[i],sub,strlen(sub)) == 0 )
        { count++ ; }
 }

Also, don't use gets, as that is prone to buffer overflows. Use fgets instead.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Note: As the compare will not exceed the length of the strings, code code use `memcmp(&str[i], sub, ...)` Likely faster with long strings YMMV. – chux - Reinstate Monica Jan 27 '16 at 17:31
  • That was very helpful for a beginner like me man. Thank you ! – Rahul Mayuranath Jan 28 '16 at 18:11
  • @RahulMayuranath Glad I could help. Feel free to [accept this answer](http://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Jan 28 '16 at 18:12
  • After making the above changes( using fgets instead of gets and adding a terimal null byte) I am now getting an output digit in excess of 30000 for every test case. What is happening ? – Rahul Mayuranath Jan 28 '16 at 18:19
  • @RahulMayuranath We would need to see the code. Please post that as a separate question. – dbush Jan 28 '16 at 18:22
  • @dbush [link](http://stackoverflow.com/questions/35069399/find-number-of-sub-strings-in-a-string) **I posted it as a separate question. ** – Rahul Mayuranath Jan 28 '16 at 18:40
0
  • Try changing your for loop from this :

    for ( i=0 ; i < strlen(str) - len ; i++ ) 
    

    to

    for ( i=0 ; i <= strlen(str) - len ; i++ ) 
    
Sagar Patel
  • 864
  • 1
  • 11
  • 22