-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_strnstr.c
41 lines (38 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/29 20:23:35 by hbaddrul #+# #+# */
/* Updated: 2021/07/30 16:31:14 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
char *haystack_tmp;
char *needle_tmp;
size_t i;
if (!ft_strlen(needle))
return ((char *)haystack);
if (!ft_strlen(haystack) || len < ft_strlen(needle))
return (0);
i = len - ft_strlen(needle) + 1;
while (i-- && *haystack)
{
haystack_tmp = (char *)haystack;
needle_tmp = (char *)needle;
while (*needle_tmp && *needle_tmp == *haystack_tmp)
{
++haystack_tmp;
++needle_tmp;
}
if (!*needle_tmp)
return ((char *)haystack);
++haystack;
}
return (0);
}