ft_strnstr

Subject

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.

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

ft_strnstr.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 */
}

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

Commented solution

ft_strnstr

Last updated

Was this helpful?