-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strnstr.c
40 lines (37 loc) · 1.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <smbaabu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 20:58:15 by smbaabu #+# #+# */
/* Updated: 2019/02/26 00:44:44 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
char *h;
char *n;
size_t l;
if (*needle == 0)
return ((char *)haystack);
n = (char *)needle;
while (*haystack && len > 0)
{
h = (char *)haystack;
l = len;
while (*haystack && *needle && *haystack == *needle && len-- > 0)
{
haystack++;
needle++;
}
if (!*needle)
return (h);
haystack = ++h;
needle = n;
len = --l;
}
return (NULL);
}