-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strnstr.c
45 lines (41 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-lo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/13 10:02:36 by adiaz-lo #+# #+# */
/* Updated: 2020/01/13 10:09:15 by adiaz-lo ### ########.fr */
/* */
/* ************************************************************************** */
/*
** This function locates the first occurrence of the substring 'to_find' in the
** string 'str' and returns a pointer to this position.
** For further information, please check the Standard C Library Function
** 'strnstr(const char *str, const char *to_find, size_t len)'.
*/
#include "libft.h"
char *ft_strnstr(const char *str, const char *to_find, size_t len)
{
size_t i;
size_t j;
if (!*to_find)
return ((char *)str);
i = 0;
while (str[i] && (size_t)i < len)
{
if (str[i] == to_find[0])
{
j = 0;
while (str[i + j] == to_find[j] && i + j < len)
{
if (to_find[j + 1] == '\0')
return ((char *)&str[i]);
j++;
}
}
i++;
}
return (0);
}