# ft\_strnstr

### Subject

{% code overflow="wrap" %}

```
STRNSTR(3) (simplified)

NAME
    strnstr -- locate a substring in a string
SYNOPSIS
    char *strnstr(const char *haystack, const char *needle, size_t len);
DESCRIPTION
    The strnstr() function locates the first occurence of the null-terminated string needle in the string haystack, where not more than len characters are searched.
    Characters that appear after a '\0' character are not searched.
RETURN VALUES
    If needle is an empty string, haystack is returned; if needle occurs nowhere in haystack, NULL is returned; otherwise a pointer to the first character of the first occurence of needle is returned.
```

{% endcode %}

### Understandable explanation

The `strnstr()` function works in the same way as `strchr()` but searches for a complete substring in max `n` characters instead of a single character.

### Hints

{% code title="ft\_strnstr.c" overflow="wrap" lineNumbers="true" %}

```c
char *ft_strnst(const char *haystack, const char *needle, size_t len)
{
    /* check if needle is empty */
        /* return haystack */
    /* loop over haystack */
        /* while current character of haystack is equal to the corresponding
         * character in needle */
             /* check if we have the complete needle */
}
```

{% endcode %}

I can also tell you that you'll need two different counters.

### Commented solution

<details>

<summary>ft_strnstr</summary>

{% code title="ft\_strnstr.c" overflow="wrap" lineNumbers="true" %}

```c
#include "libft.h"

char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
    size_t i;
    size_t j;
    
    i = 0;
    j = 0;
    /* checking if needle is empty */
    if (needle[0] == 0)
        return ((char *) haystack);
    /* loop over haystack while we haven't looped over the whole
     * thing or until i = len
     */
    while (haystack[i] && i < len)
    {
        /* inside this first while loop
         * we are looping over the haystack as long as 
         * the current character is the correct character
         * in the needle
         */
        while (haystack[i + j] == needle[j] && haystack[i + j] && i + j < len)
        {
            /* we are inside this loop while the current
             * haystack character is the correct character in the 
             * needle so we just increment j to offset our haystack
             */
            j++;
            /* if needle[j] == 0, this means we read the whole
             * needle, so we found it in the haystack
             * we can return a pointer to the first character of
             * the needle
             * that why we used 2 counters, now we can offset our 
             * haystack pointer by i, which is the position of the
             * first character of the needle
             */
            if (needle[j] == 0)
                return ((char *) haystack + i);
        }
        i++;
        /* don't forget to set j to 0 again, otherwise you will
         * be offsetted in the needle beginning from the second
         * haystack character
         */
        j = 0;
    }
    return (0);
}
```

{% endcode %}

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-cursus.gitbook.io/guide/0-rank-00/libft/libc-functions/ft_strnstr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
