-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
46 lines (40 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tauer <tauer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 15:43:37 by tauer #+# #+# */
/* Updated: 2023/11/06 15:43:37 by tauer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *s1, const char *s2, size_t len)
{
size_t s1_size;
size_t s2_size;
if (ft_strlen(s2) == 0)
return ((char *)s1);
if (!len)
return (NULL);
s1_size = ft_strlen(s1);
s2_size = ft_strlen(s2);
if (s1_size < len)
len = s1_size;
while (*s1 && len >= s2_size)
{
if (ft_strncmp(s1, s2, s2_size) == 0)
return ((char *)s1);
s1++;
len--;
}
return (NULL);
}
// int main(void)
// {
// const char *str1 = "salut les copains";
// const char *str2 = "copains";
// printf("%s\n", ft_strnstr(str1, str2, 8));
// return (0);
// }