-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
55 lines (50 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maareval <maareval@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/27 18:39:38 by maareval #+# #+# */
/* Updated: 2022/04/28 21:29:45 by maareval ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <string.h>
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
size_t i;
size_t j;
if (!*s2)
return ((char *)s1);
i = 0;
while (s1[i] && (size_t)i < n)
{
if (s1[i] == s2[0])
{
j = 0;
while (s1[i + j] == s2[j] && i + j < n)
{
if (s2[j + 1] == '\0')
return ((char *)&s1[i]);
j++;
}
}
i++;
}
return (0);
}
/*
int main (void)
{
const char s1[];
const char s2[];
size_t n;
s1[] = "Hola";
s2[] = "Ho";
n = 8;
printf("%s\n", strnstr (s1, s2, n));
return (0);
}
*/