-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
60 lines (55 loc) · 1.62 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/31 12:35:49 by rbetz #+# #+# */
/* Updated: 2022/03/31 17:43:24 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *find_it(char *h, char *n, size_t nl, size_t sl)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (h[i] != '\0' && i < sl)
{
if (h[i] == n[0])
{
while (j < nl && i + j < sl)
{
if (h[i + j] == n[j])
j++;
else
{
j = 0;
break ;
}
}
if (j == nl)
return ((char *)&h[i]);
}
i++;
}
return (NULL);
}
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t k;
size_t l;
char *hay;
char *nee;
k = ft_strlen(needle);
l = ft_strlen(haystack);
hay = (char *)haystack;
nee = (char *)needle;
if (k > l || k > len)
return (NULL);
if (haystack == NULL || needle == NULL || len < 1 || l == 0 || k == 0)
return (hay);
return (find_it(hay, nee, k, len));
}