-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
executable file
·49 lines (44 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tseguier <tseguier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 08:46:01 by tseguier #+# #+# */
/* Updated: 2014/03/27 18:40:32 by jcoignet ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
static int ft_str_dismatch(const char *str1, const char *str2)
{
size_t i;
i = 0;
while (str2[i] != '\0')
{
if (str1[i] != str2[i])
return (1);
else
++i;
}
return (0);
}
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
unsigned int i;
size_t s2len;
if (!s1 || !s2)
return (NULL);
s2len = ft_strlen(s2);
i = 0;
if (*s2 == '\0')
return ((char *)s1);
while (s1[i] != '\0' && s2len + i <= n)
{
if (ft_str_dismatch(&s1[i], s2) == 0)
return ((char *)&s1[i]);
++i;
}
return (NULL);
}