-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strstr.c
60 lines (55 loc) · 1.57 KB
/
ft_strstr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 15:30:57 by rbetz #+# #+# */
/* Updated: 2022/07/18 15:57:07 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *find_str(char *h, char *n, size_t nl)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (h[i] != '\0')
{
if (h[i] == n[0])
{
while (j < nl && h[i + j] != '\0')
{
if (h[i + j] == n[j])
j++;
else
{
j = 0;
break ;
}
}
if (j == nl)
return ((char *)&h[i]);
}
i++;
}
return (NULL);
}
char *ft_strstr(const char *haystack, const char *needle)
{
size_t k;
size_t l;
char *hay;
char *nee;
k = ft_strlen(needle);
l = ft_strlen(haystack);
hay = (char *)haystack;
nee = (char *)needle;
if (k > l)
return (NULL);
if (haystack == NULL || needle == NULL || l == 0 || k == 0)
return (hay);
return (find_str(hay, nee, k));
}