-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
51 lines (47 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeshin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/06 12:36:05 by jeshin #+# #+# */
/* Updated: 2023/10/20 16:07:19 by jeshin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_same_str(const char *s1, const char *s2, size_t n)
{
while (n && *s1 && *s2)
{
if (*s1 != *s2)
return (1);
s1++;
s2++;
n--;
}
if (*s2 && (!n || !*s1))
return (-1);
return (0);
}
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
int check;
i = 0;
if (!*needle)
return ((char *)haystack);
while (haystack[i] && i < len)
{
if (haystack[i] == *needle)
{
check = is_same_str(&haystack[i], needle, len - i);
if (!check)
return (&((char *)haystack)[i]);
else if (check == -1)
return (0);
}
i++;
}
return (0);
}