-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
48 lines (45 loc) · 1.48 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: fvieira <fvieira@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/17 11:27:31 by fvieira #+# #+# */
/* Updated: 2022/08/22 15:21:18 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
size_t i;
size_t i_s2;
size_t len_needle;
i = 0;
len_needle = ft_strlen(s2);
if (n == 0 && !s1)
return (0);
if (s2[0] == '\0')
return ((char *) s1);
while (s1[i] != '\0' && i < n)
{
i_s2 = 0;
if (s1[i] == s2[0])
{
while (s1[i + i_s2] == s2[i_s2] && s2[i_s2] != '\0' && i + i_s2 < n)
{
if (len_needle - 1 == i_s2)
return ((char *) s1 + i);
i_s2++;
}
}
i++;
}
return (0);
}
/*#include <stdio.h>
int main(void)
{
printf("%s",ft_strnstr("lorem ipsum dolor sit amet", "dolor", 15));
return (0);
}*/