-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
38 lines (35 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
38
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvon-nag <fvon-nag@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/24 08:27:48 by fvon-nag #+# #+# */
/* Updated: 2022/12/15 13:13:54 by fvon-nag ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t c;
i = 0;
if (len == 0 && needle[0] != '\0')
return (0);
if (needle[0] == '\0' || len == 0)
return ((char *) haystack);
while (haystack[i] != '\0' && i < len)
{
c = 0;
while (haystack[i + c] != '\0' && haystack[i + c] == needle[c]
&& i + c < len)
{
if (needle[c + 1] == '\0')
return ((char *)&haystack[i]);
c++;
}
i++;
}
return (0);
}