-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
53 lines (48 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hhagiwar <hhagiwar@student.42Tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/04 14:35:24 by hhagiwar #+# #+# */
/* Updated: 2023/06/16 13:02:01 by hhagiwar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_pointer(const char *str, const char *to_find, size_t len)
{
size_t i;
size_t j;
size_t size;
i = 0;
j = 0;
size = ft_strlen(to_find);
while (str[i] != '\0' && i < len && size <= len - i)
{
if (str[i] == to_find[0])
{
while (str[i + j] == to_find[j] && j < len)
{
if (j == size - 1)
return (i);
j++;
}
}
j = 0;
i++;
}
return (-1);
}
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
int pointer;
if (haystack == NULL && len < 1)
return (NULL);
else if (needle[0] == '\0')
return ((char *)haystack);
pointer = is_pointer(haystack, needle, len);
if (pointer == -1)
return (NULL);
return ((char *)haystack + pointer);
}