-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathft_strnstr.c
49 lines (44 loc) · 1.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 16:38:13 by mihykim #+# #+# */
/* Updated: 2020/04/03 22:07:29 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** - Locates 1st occurrence of 'needle' in 'haystack'
** searching not more than len.
** - Characters that appear after a `\0' character are not searched.
** - Returns
** 1) haystack, if needle is an empty string.
** 2) a pointer to first character of first occurrence of needle.
** 2) NULL, if needle occurs nowhere in haystack.
*/
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
if (needle[0] == 0)
return ((char *)haystack);
i = 0;
while (haystack[i] && i < len)
{
j = 0;
while (haystack[i + j] == needle[j] && i + j < len)
{
j++;
if (needle[j] == 0)
return ((char *)haystack + i);
}
i++;
}
return (0);
}
/*
** line 35 : add while condition, 'i + j' not to exceed 'len'
*/