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 ;
}  
6
  • This code doesn't make any sense. Why do you need to make a copy of the sub string in the first place? Commented Jan 27, 2016 at 14:09
  • use fgets instead of gets unless there is a very good reason. Commented Jan 27, 2016 at 14:15
  • Use a debugger or at least print the data you check. Commented Jan 27, 2016 at 14:30
  • @ForeverStudent: Which reason would rectify to use an unsafe function with not more features? Commented Jan 27, 2016 at 14:31
  • 1
    @Olaf: stupid ones we all heard before: fgets not allowed by assignment, fgets not supported by our custom compiler/runtime, etc Commented Jan 27, 2016 at 15:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
That was very helpful for a beginner like me man. Thank you !
@RahulMayuranath Glad I could help. Feel free to accept this answer if you found it useful.
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 ?
@RahulMayuranath We would need to see the code. Please post that as a separate question.
|
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++ ) 
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.