-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
51 lines (47 loc) · 1.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chrhuang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/07 14:16:09 by chrhuang #+# #+# */
/* Updated: 2018/11/12 14:41:37 by chrhuang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check(const char *str, const char *find, size_t n)
{
char *tmp;
char *tmp1;
tmp = (char *)str;
tmp1 = (char *)find;
while (*tmp != '\0' && n != 0)
{
if (*tmp != *tmp1 && *tmp1 == '\0')
return (0);
else if (*tmp != *tmp1)
break ;
tmp = tmp + 1;
tmp1 = tmp1 + 1;
n = n - 1;
}
if (*tmp == *tmp1 && *tmp == '\0' && *tmp1 == '\0')
return (0);
else if (n == 0 && *tmp1 == '\0')
return (0);
return (-1);
}
char *ft_strnstr(const char *str, const char *find, size_t n)
{
if (ft_strcmp(find, "") == 0)
return ((char *)str);
while (*str != '\0' && n != 0)
{
if ((*str == *find) && check(str, find, n) == 0)
return ((char *)str);
str = str + 1;
n = n - 1;
}
return (NULL);
}