ft_strnstr.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ft_strnstr.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <marvin@42.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2022/07/08 20:15:05 by gychoi #+# #+# */ 9 /* Updated: 2022/07/11 15:00:25 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "libft.h" 14 15 char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 { 17 size_t i; 18 size_t j; 19 20 if (needle[0] == '\0') 21 return ((char *)haystack); 22 i = 0; 23 while (haystack[i] != '\0' && i < len) 24 { 25 j = 0; 26 while (haystack[i + j] == needle[j] && i + j < len) 27 { 28 j++; 29 if (needle[j] == '\0') 30 return ((char *)haystack + i); 31 } 32 i++; 33 } 34 return (NULL); 35 }