-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
39 lines (36 loc) · 1.24 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brpereir <brpereir@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 21:45:01 by brpereir #+# #+# */
/* Updated: 2023/04/23 19:03:34 by brpereir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(char *haystack, char *needle, int n)
{
int i;
int j;
i = 0;
if (needle[0] == '\0')
return (haystack);
if (!n)
return (NULL);
while (haystack[i] && i < n)
{
j = 0;
while (haystack[i + j] == needle[j] && ((i + j) < n))
{
if (needle[j + 1] == '\0')
{
return (&haystack[i]);
}
j++;
}
i++;
}
return (0);
}