-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_strnstr.c
49 lines (44 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yuotsuka <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 21:05:41 by yuotsuka #+# #+# */
/* Updated: 2024/04/19 00:34:24 by yuotsuka ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t needle_len;
if (*needle++)
{
needle_len = ft_strlen(needle);
while (1)
{
while (1)
{
if (len-- < 1 || !*haystack++)
return (NULL);
if (*(haystack - 1) == *(needle - 1))
break ;
}
if (needle_len > len)
return (NULL);
if (!ft_strncmp(haystack, needle, needle_len))
break ;
}
haystack--;
}
return ((char *)haystack);
}
// int main(void)
// {
// char haystack[] = "This is a test!";
// char needle[] = "is";
// printf("Haystack: %s\nNeedle:%s\nWhere:%s",
// haystack, needle, ft_strnstr(haystack, needle, 4));
// return (0);
// }