-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strnstr.c
More file actions
50 lines (43 loc) · 1.5 KB
/
ft_strnstr.c
File metadata and controls
50 lines (43 loc) · 1.5 KB
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
48
49
50
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 19:52:33 by marvin #+# #+# */
/* Updated: 2022/05/19 19:52:35 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t h;
size_t n;
h = 0;
if (needle[0] == '\0')
return ((char *)haystack);
while (haystack[h])
{
n = 0;
while (haystack[h + n] == needle[n] && (h + n) < len)
{
if (haystack[h + n] == '\0' && needle[n] == '\0')
return ((char *)&haystack[h]);
n++;
}
if (needle[n] == '\0')
return ((char *)haystack + h);
h++;
}
return (NULL);
}
/*int main()
{
const char *largestring = "AAB Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = ft_strnstr(largestring, smallstring, 7);
printf("%s", ptr);
return 0;
}*/