-1

I'm implementing the C "strstr" function in C. This function takes 2 string in parameter and tell whether or not, the 1st string contains the second one. However, while the result is expected to be false, it returns true. Could you give me an explanation, please ?

Here is code :

#include <stdio.h>
#include <string.h>

int searchStr(char *ch1, char *ch2);

int searchStr(char *ch1, char *ch2)
{
    int i = 0;
    int j = 0;
    while (i < strlen(ch1) - 1)
    {
        while (j < strlen(ch2) - 1)
        {
            if(i == strlen(ch1) - 1) 
            {
                return 0;
            }
            else
            {
              if (ch1[i] == ch2[j])
            {
                j++;
                i++;
            }
            else
            {
                if (j > 0)
                {
                    j = 0;
                }
                else
                {
                    i++;
                }
            } 
            }
        }
        return 1;
    }
}

int main()
{
    printf("%d", searchStr("science", "sh"));

} 

Regards

YT

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • That double while loop is too complicated for me. How about you put `return -2` at the end of the program, and see if maybe you miss some cases? – Victor Eijkhout Sep 26 '21 at 13:27
  • Is that `return 1` exactly where you want? Are there any possible logic paths *without* a `return`? – Bob__ Sep 26 '21 at 13:46
  • 2
    Starting with `i` = 0 and `j` = 0, your code compares the first characters, which are equal. Then it increments `j` to 1 and `i` to 1. Then it performs the loop test `j < strlen(ch2) - 1`. Since `ch2` is “sh”, its length is 2, so `strlen(ch2) - 1` is 1, and `j` is not less than 1, so the loop ends, and the code returns 1. Rewrite your code so that `i` iterates through each possible starting position and is never changed inside the loop (e.g., while testing a substring), only when going on to the next starting position. Fix the loop on `j` to check all of `ch2`. – Eric Postpischil Sep 26 '21 at 13:46

2 Answers2

1

It is because the function definition does not make a sense.:)

For this while loop

while (i < strlen(ch1) - 1)
{

this if statement in the inner while loop

        if(i == strlen(ch1) - 1) 

always evaluates to logical false when strlen( ch2 ) is equal to 2.

Thus this return statement

return 0;

never gets the control in this case.

That is the inner while loop will have only one iteration and when the loop gets the control i is less than strlen(ch1) - 1 due to the condition of the outer while loop.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Few points to note

  • loop from 0 to strlen(ch1)-1 doesnt loop until final character
  • return 1; is at the end of the outer while loop. So it execute as soon as exit from the first loop. Due to that outer loop only executes for one time and in that time it return 1.This is why it always return 1.

I add some changes to your code as follows.Also note the usage of break statement.

#include <stdio.h>
#include <string.h>

int searchStr(char *ch1, char *ch2);

int searchStr(char *ch1, char *ch2)
{
    int isContain = 0;
    int i = 0;
    int j = 0;
    
    while (i < strlen(ch1) )
    {

        int k = i;
        isContain = 1;
        
        if(i == strlen(ch1) - 1) {
            
            if(strlen(ch2) == 1 && ch1[k] == ch2[j]){
                return 1;    
            }
            else{
                return 0;    
            }
            
        }
        
            
      
            
        while(j < strlen(ch2) ){
            
            if(ch1[k] != ch2[j]){
                isContain = 0;
                j=0;
                break;
            }else{
                j++;
                k++;
            }
        
        }
        
        if(isContain == 1){
            return 1;
        }
        
        i++;
    }
}

int main()
{
    printf("%d", searchStr("scince", "h"));

} 

Also you can use stack instead of two loops for this task which is much efficient than this.