-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathft_strnstr.c
48 lines (44 loc) · 1.65 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/04 14:36:51 by apuchill #+# #+# */
/* Updated: 2020/02/19 15:13:39 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: <string.h>
** SYNOPSIS: locate a substring in a string (size-bounded)
**
** DESCRIPTION:
** The strnstr() function locates the first occurrence of the null-termi-
** nated string s2 in the string s1, where not more than n characters are
** searched. Characters that appear after a `\0' character are not
** searched.
*/
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t h;
size_t n;
h = 0;
if (needle[0] == '\0')
return ((char *)haystack);
while (haystack[h] != '\0')
{
n = 0;
while (haystack[h + n] == needle[n] && (h + n) < len)
{
if (haystack[h + n] == '\0' && needle[n] == '\0')
return ((char *)&haystack[h]);
n++;
}
if (needle[n] == '\0')
return ((char *)haystack + h);
h++;
}
return (0);
}