-6

I'm trying to build my strstr() function, it's look all fine but when I execute the code it return nothing ;

This is my function :

#include <stdio.h>

char* ft_strstr(char *str, char *to_find) {
  char *a;
  char *b;

  b = to_find;
  if (!*b)
    return (char*) str;
  while (*str) {
    if (*str != *b)
      continue;
    a = str;
    while (1) {
      if (!*b)
        return (char*) str;
      if (*a++ != *b++)
        break;
    }
    str++;
    b = to_find;
  }
  return (NULL);
}

int main() {
  char string[] = "Hello from morroco";
  char find[] = "from";
  ft_strstr(string, find);
  printf("%s", find);
  return 0;
}

I don't know what. I have to try to fix it cause it looks all fine for me.

8
  • OT: Regarding return (char *)str;... If str already have the type char *, there's just no need to do an explicit conversion to the same type. Commented Sep 20, 2023 at 11:33
  • 4
    Please format your code properly. Readable code is very important, especially for beginners. Commented Sep 20, 2023 at 11:33
  • 3
    The code doesn't modify the string pointed to by the to_find argument. Why should find in the main function change? You seem to have some basic misunderstandings about both the strstr function as well as the basics of C. Commented Sep 20, 2023 at 11:34
  • 1
    At a glance you seem to have some 2-3 bugs caused by sloppy formatting alone... Commented Sep 20, 2023 at 11:36
  • 1
    What exactly do you expect to happen when you reach continue;? What situation will you be in when you come to if(*str != *b) again? How is it different from last time you got there? Commented Sep 20, 2023 at 11:43

1 Answer 1

2

There is a bug in this if statement

if(*str != *b)
    continue;

the pointer str is not increased in this case and as a result the loop will be infinite.

instead of the while loop it is better to use a for loop as

for ( ; *str; ++str )

or the while loop can look like

while (*str)
{
    if( *str == *b )
    {
        a = str;
        while(1)
        {
            if (!*b )
                return (char *)str;
            if (*a++ != *b++)
                break;
        }
    }
    str++;
    b = to_find;
}

Also it seems you mean

char *pos = ft_strstr(string , find);
if ( pos ) printf("%s", pos);

instead of

ft_strstr(string , find);
printf("%s", find);

Pay attention to that the function should be declared like

char * ft_strstr( const char *str, const char *to_find);

And using such castings like

return (char *)str;

makes sense if the parameter str has the type const char * as it should have.

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

7 Comments

Thank you vlad , but in this subject for loop not allowed , cause of that i have to use while but i incremenet str++ in the last of the first while loop and i tried to stock the function in *p and print it with condition but the output still the same ,
i have to notice that i can't declare the params in the str function as a const
@OmarGhazi There s nothing prevents you to declare the parameters with the qualifer const. Also the variables a and b must be declared with the qualifier const.
@OmarGhazi — why can't you declare function parameters as const? Why is your "teacher" abusing the language so? It bodes ill for you — you should be being taught how to write good C code, and good C code uses const whenever sensible. Are you also constrained to C90? If so, you have to start worrying about the standard of education you are receiving.
@OmarGhazi restrictions concerning for and const deserve to be in the question.
|

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.