-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
47 lines (40 loc) · 1.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-haib <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 13:15:15 by ael-haib #+# #+# */
/* Updated: 2024/02/01 01:33:05 by ael-haib ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t needle_len;
needle_len = ft_strlen(needle);
if (needle_len == 0)
{
return ((char *)haystack);
}
while (*haystack && len >= needle_len)
{
if (*haystack == *needle && ft_strncmp(haystack, needle,
needle_len) == 0)
return ((char *)haystack);
haystack++;
len--;
}
return (NULL);
}
/* int main() {
const char *haystack = "Hello, World!";
const char *needle = "World";
size_t n;
n = 12;
if (ft_strnstr(haystack, needle, n))
printf("i found it %s\n", ft_strnstr(haystack, needle, n));
else (printf("not found it\n"));
return (0);
} */