-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
62 lines (56 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpicoli- < lpicoli-@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 11:19:34 by lpicoli- #+# #+# */
/* Updated: 2022/11/24 12:34:33 by lpicoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check_str(char *big, char *little, size_t i)
{
int j;
j = 0;
while (little[j])
{
if (big[i + j] != little[j])
return (0);
j++;
}
return (1);
}
char *ft_strnstr(const char *big, const char *little, size_t len)
{
char *big_str;
char *little_str;
size_t little_len;
size_t i;
big_str = (char *)big;
little_str = (char *)little;
i = 0;
little_len = ft_strlen(little);
if (little_str[0] == '\0')
return (big_str);
if (big_str[0] == '\0')
return (NULL);
while (little_len + i <= len)
{
if (big_str[i] == little_str[0])
{
if (ft_check_str(big_str, little_str, i))
return (&big_str[i]);
}
i++;
}
return (NULL);
}
/*
int main()
{
char *ptr;
ptr = ft_strnstr("", "ola", 10);
printf("%s", ptr);
} */