-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
48 lines (44 loc) · 1.56 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
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: estettle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 11:48:24 by estettle #+# #+# */
/* Updated: 2024/10/11 11:48:25 by estettle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* ft_strnstr()
* locates a substring needle inside the haystack string.
* Returns : If needle is empty, haystack, if no needle in haystack, NULL,
* otherwise a pointer to the first character of the substring found.
*/
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
if (!*needle)
return ((char *)haystack);
i = 0;
j = 0;
while (i < len && *haystack)
{
j = 0;
while (j + i < len && haystack[j] && haystack[j] == needle[j])
j++;
if (!needle[j])
return ((char *)haystack);
haystack++;
i++;
}
return (NULL);
}
/*
#include <stdio.h>
int main(void)
{
printf("%s\n", ft_strnstr("Can. You. Hear. Me?", "", 30));
}
*/