-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
executable file
·44 lines (40 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngouy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 13:54:14 by ngouy #+# #+# */
/* Updated: 2015/11/04 17:16:13 by ngouy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Find the first occurence of firsts n bytes of s2 in s1
*/
#include "libft.h"
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
int i;
int k;
int j;
if (s1[0] == s2[0] && s1[0] == '\0')
return ((char *)s1);
i = 0;
j = ft_strlen(s2);
while (s1[i] && i < (int)n)
{
k = 0;
while (s1[i + k] == s2[k] && i + k < (int)n)
{
if (s2[k] == '\0')
return ((char *)s1 + i);
k++;
}
if (k == j)
return ((char *)s1 + i);
else
i++;
}
return (NULL);
}