-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
58 lines (53 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: polmarti <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/19 10:14:22 by polmarti #+# #+# */
/* Updated: 2023/09/19 10:14:26 by polmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
char *s;
char *tf;
size_t i;
size_t tmp;
i = 0;
if (haystack == needle || !*needle)
return ((char *)haystack);
while (*haystack && i < len)
{
s = (char *)haystack;
tf = (char *)needle;
tmp = 0;
while (*s && *tf && *s == *tf && tmp + i < len)
{
++s;
++tf;
tmp++;
}
if (!*tf)
return ((char *)haystack);
++haystack;
i++;
}
return (0);
}
/*int main(void)
{
char *s1 = "aaabcabcd";
char *s2 = "cd";
//char *s1 = "MZIRIBMZIRIBMZE123";
//char *s2 = "MZIRIBMZE";
//size_t max = strlen(s2);
//printf("%zu\n", max);
printf("%s\n", strnstr("aabcabc", "aabc", -1));
printf("%s\n", ft_strnstr("aabcabc", "aabc", -1));
//printf("%s\n", ft_strnstr(dest, to_find, 0));
//printf("%s\n", strnstr(dest, to_find, 0));
return (0);
}*/