Home > Enterprise >  Why does my program return true as the result is expected to be false?
Why does my program return true as the result is expected to be false?

Time:09-27

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related