-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
74 lines (70 loc) · 2.76 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
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbutt <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/03 19:19:34 by mbutt #+# #+# */
/* Updated: 2019/03/28 11:14:17 by mbutt ### ########.fr */
/* */
/* ************************************************************************** */
/*
** The strstr() function locates the first occurrence of the null-terminated
** string needle in the null-terminated string haystack.
** 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.
** 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)
{
int i;
size_t haystack_len;
size_t needle_len;
haystack_len = ft_strlen(haystack);
needle_len = ft_strlen(needle);
i = 0;
if (len > haystack_len && (needle_len > haystack_len))
return (NULL);
else if (haystack_len < 1 && needle_len < 1)
return ((char *)haystack);
else
{
while (len >= needle_len)
{
if (ft_strncmp(haystack + i, needle, needle_len) == 0)
return ((char *)haystack + i);
i++;
len--;
}
}
return (NULL);
}
/*
** int main (void)
** {
** char *string1 = "123456789";
** char *string2 = "123";
** size_t max = 9;
** char buf[10];
** bzero(buf, 10);
** strcpy(buf, "un deux 9");
** printf(" strnstr:|%s|\n", strnstr(string1, string2, max));
** printf("ft_strnstr:|%s|\n", ft_strnstr(string1, string2, max));
** printf("\n\n");
** printf(" strnstr:|%s|\n", strnstr(buf, "deux", 10));
** printf("ft_strnstr:|%s|\n\n", ft_strnstr(buf, "deux", 10));
** printf("After buf[9] = '6' and buf[1] = 0\n");
** printf("buff[9] = '6' writes onto the null terminator\n");
** printf("buff[1] = 0 is equal to null on Ascii table\n\n");
** buf[9] = '6';
** buf[1] = 0;
** printf(" strnstr:|%s|\n", strnstr(buf, "deux", 10));
** printf("ft_strnstr:|%s|\n", ft_strnstr(buf, "deux", 10));
** return(0);
** }
*/