-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_strnstr.c
More file actions
51 lines (44 loc) · 1.51 KB
/
ft_strnstr.c
File metadata and controls
51 lines (44 loc) · 1.51 KB
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: jony <jony@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/21 17:22:57 by mhasan #+# #+# */
/* Updated: 2019/11/04 22:03:00 by jony ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *str, const char *to_find, size_t len)
{
unsigned int i;
unsigned int j;
if (to_find[0] == '\0')
return ((char *)str);
i = 0;
while (str[i] != '\0' && i < len)
{
j = 0;
while (str[i + j] == to_find[j] && (i + j) < len)
{
if (to_find[j + 1] == '\0')
{
return ((char *)str + i);
}
j++;
}
i++;
}
return (0);
}
int main()
{
char haystack[20] = "TutorialsPoint";
char needle[10] = "als";
char *ret;
ret = ft_strnstr("lorem ipsum dolor sit amet", "sit", 10);
printf("%s\n", ret);
printf("%s", ft_strnstr("lorem ipsum dolor sit amet", "dolor", 17));
return (0);
}