-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
78 lines (74 loc) · 2.19 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/31 13:38:24 by sde-silv #+# #+# */
/* Updated: 2023/06/12 16:18:41 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
include ft_putendl_fd.c when testing main
*/
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (little[0] == '\0')
return ((char *)big);
while (big[i] != '\0' && i < len)
{
j = 0;
while (big[i + j] == little[j] && i + j < len)
{
if (little[j + 1] == '\0')
return ((char *)big + i);
j ++;
}
i ++;
}
return (NULL);
}
/*
int main(void)
{
char *ptr;
ptr = ft_strnstr("Hello World!", "l", 0);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", 1);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", 2);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", 11);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", 12);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", 15);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("", "l", 3);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "", 15);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", -1);
if (ptr)
ft_putendl_fd(ptr, 1);
ptr = ft_strnstr("Hello World!", "l", -200);
if (ptr)
ft_putendl_fd(ptr, 1);
else
ft_putendl_fd("Function returned NULL", 1);
return (0);
}
*/