-
Notifications
You must be signed in to change notification settings - Fork 9
/
ft_strnstr.c
38 lines (35 loc) · 1.33 KB
/
ft_strnstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rchallie <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 16:13:35 by rchallie #+# #+# */
/* Updated: 2019/10/08 16:42:01 by rchallie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t c;
size_t n_len;
char *hay;
i = 0;
hay = (char *)haystack;
n_len = ft_strlen(needle);
if (n_len == 0 || haystack == needle)
return (hay);
while (hay[i] != '\0' && i < len)
{
c = 0;
while (hay[i + c] != '\0' && needle[c] != '\0'
&& hay[i + c] == needle[c] && i + c < len)
c++;
if (c == n_len)
return (hay + i);
i++;
}
return (0);
}