-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
50 lines (45 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: retahri <retahri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 01:00:52 by retahri #+# #+# */
/* Updated: 2024/11/02 12:46:29 by retahri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t idx_hay;
size_t idx_nedl;
idx_hay = 0;
idx_nedl = 0;
if (!*needle)
return ((char *)haystack);
if (!*haystack)
return (NULL);
while (haystack[idx_hay] && idx_hay < len)
{
while (haystack[idx_hay + idx_nedl] == needle[idx_nedl]
&& haystack[idx_hay + idx_nedl] && idx_nedl + idx_hay < len)
{
idx_nedl++;
if (needle[idx_nedl] == '\0')
return ((char *)haystack + idx_hay);
}
idx_hay++;
idx_nedl = 0;
}
return (NULL);
}
// #include <stdio.h>
// #include <string.h>
// int main()
// {
// char *str = "reda";
// char *to = "re";
// printf("O: %s\n", strnstr(str, to, 1));
// printf("M: %s\n", ft_strnstr(str, to, 1));
// }