-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
47 lines (42 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
42
43
44
45
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smurad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/17 05:49:26 by smurad #+# #+# */
/* Updated: 2019/12/18 10:24:55 by smurad ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int checkstr(const char *haystack, const char *needle)
{
int i;
i = 0;
while (needle[i])
{
if (haystack[i] == needle[i])
i++;
else
return (0);
}
return (1);
}
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t lenneedle;
lenneedle = ft_strlen(needle);
i = 0;
if (needle[0] == '\0')
return ((char *)haystack);
while (haystack[i] && i < len)
{
if (haystack[i] == needle[0] && (lenneedle + i) <= len &&
checkstr((&haystack[i]), needle))
return ((char *)&haystack[i]);
i++;
}
return (NULL);
}