Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_strnstr.c File Reference
#include <stdlib.h>
+ Include dependency graph for ft_strnstr.c:

Go to the source code of this file.

Functions

char * ft_strnstr (const char *haystack, const char *needle, size_t len)
 Finds a substring within a bounded string region.
 

Function Documentation

◆ ft_strnstr()

char * ft_strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

Finds a substring within a bounded string region.

Searches needle in haystack but not beyond len bytes.

Parameters
haystackString to search in.
needleSubstring to search for.
lenMaximum number of bytes to inspect in haystack.
Returns
Pointer to first match, or NULL when not found.

Definition at line 25 of file ft_strnstr.c.

26{
27 size_t i;
28 size_t j;
29
30 if (!*needle)
31 return ((char *)haystack);
32 i = 0;
33 while (haystack[i] && i < len)
34 {
35 j = 0;
36 while (haystack[i + j] && needle[j] && i + j < len
37 && haystack[i + j] == needle[j])
38 j++;
39 if (!needle[j])
40 return ((char *)&haystack[i]);
41 i++;
42 }
43 return (NULL);
44}