-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
63 lines (56 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: coskelet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 19:37:04 by coskelet #+# #+# */
/* Updated: 2021/11/09 12:42:21 by coskelet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_min(size_t *lens);
static char *find_str(const char *big, const char *little, size_t *lens);
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t lens[4];
if ('\0' == *little)
return ((char *)big);
lens[0] = len;
lens[1] = ft_strlen(big);
lens[2] = ft_strlen(little);
lens[3] = get_min(lens);
if (lens[1] < lens[2])
return (NULL);
return (find_str(big, little, lens));
}
static char *find_str(const char *big, const char *little, size_t *lens)
{
size_t i;
size_t j;
i = 0;
while ((lens[2] + i) <= lens[3] && *(big + i) != '\n')
{
j = 0;
while (*(little + j) != '\n' && j <= lens[0])
{
if (j == lens[2])
return ((char *)(big + i));
if (*(big + i + j) != *(little + j))
break ;
j++;
}
i++;
}
return (NULL);
}
static size_t get_min(size_t *lens)
{
if (lens[1] < lens[0])
{
return (lens[1]);
}
else
return (lens[0]);
}