minishell
ft_strnstr.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ft_strnstr.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: kfujita <kfujita@student.42tokyo.jp> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2022/04/10 16:52:18 by kfujita #+# #+# */
9 /* Updated: 2022/04/19 06:34:37 by kfujita ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include <stddef.h>
14 
15 char *ft_strnstr(const char *haystack, const char *needle, size_t len)
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 }
char * ft_strnstr(const char *haystack, const char *needle, size_t len)
Definition: ft_strnstr.c:15