minishell
ft_strnstr.c File Reference
#include <stddef.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)
 

Function Documentation

◆ ft_strnstr()

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

Definition at line 15 of file ft_strnstr.c.

16 {
17  size_t pos;
18 
19  if (*needle == '\0')
20  return ((char *)haystack);
21  while (len > 0 && *haystack != '\0')
22  {
23  if (*haystack == *needle)
24  {
25  pos = 0;
26  while (needle[pos] != '\0'
27  && len > pos && haystack[pos] == needle[pos])
28  pos++;
29  if (needle[pos] == '\0')
30  return ((char *)haystack);
31  }
32  haystack++;
33  len--;
34  }
35  return (NULL);
36 }