-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strnstr.c
52 lines (48 loc) · 1.93 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
51
52
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/30 20:25:21 by cado-car #+# #+# */
/* Updated: 2021/07/31 11:33:29 by cado-car ### ########lyon.fr */
/* */
/* ************************************************************************** */
/*
* LIBRARY
* #include <string.h>
* DESCRIPTION
* The strnstr() function locates the first occurrence of the null-terminated
* string needle in the string haystack, where not more than len characters are
* searched. Characters that appear after a `\0' character are not searched.
* PARAMETERS
* #1. The string in which to check for the occurence of the substring.
* #2. The substring to check the occurence of.
* #3. The number of bytes to check in haystack.
* RETURN VALUES
* If needle is an empty string, haystack is returned; if needle occurs nowhere
* in haystack, NULL is returned; otherwise a pointer to the first character of
* the first occurrence of needle is returned.
*/
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t c;
if (needle[0] == '\0')
return ((char *)haystack);
i = 0;
while (haystack[i] != '\0' && i < len)
{
c = 0;
while ((haystack[i + c] == needle[c]) && (i + c) < len)
{
if (needle[c + 1] == '\0')
return ((char *)(&haystack[i]));
c++;
}
i++;
}
return (NULL);
}