-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
37 lines (34 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelphias <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/27 16:05:44 by aelphias #+# #+# */
/* Updated: 2019/09/29 19:14:53 by aelphias ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
unsigned int loc;
unsigned int i;
if (!*needle)
return ((char*)haystack);
loc = 0;
while (haystack[loc] != '\0' && (size_t)loc < len)
{
if (haystack[loc] == needle[0])
{
i = 1;
while (needle[i] != '\0' && haystack[loc + i] == needle[i] &&
(size_t)(loc + i) < len)
++i;
if (needle[i] == '\0')
return ((char*)&haystack[loc]);
}
++loc;
}
return (0);
}