-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_strnstr.c
53 lines (48 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jede-ara <jede-ara@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 15:41:00 by jede-ara #+# #+# */
/* Updated: 2022/11/08 14:30:29 by jede-ara ### ########.fr */
/* */
/* ************************************************************************** */
/*
DESCRIÇÃO: strnstr() localiza a primeira ocorrência da string s2 terminada
em nulo na string s1, onde não mais do que n caracteres são pesquisados.
Os caracteres que aparecem após um caractere nulo não são pesquisados.
*/
#include "libft.h"
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
size_t a;
size_t j;
a = 0;
if ((n == 0 && !*s2) || !*s2)
return ((char *)s1);
if (n == 0)
return (NULL);
while (s1[a] != '\0' && a < n)
{
j = 0;
while (s1[a + j] != '\0' && s2[j] != '\0'
&& s1[a + j] == s2[j] && (a + j) < n)
{
if (s2[j + 1] == '\0')
return ((char *)&(s1[a]));
j++;
}
a++;
}
return (NULL);
}
/*#include <stdio.h>
int main()
{
char * param_1 = "hello";
char * param_2 = "ello";
printf("This is the string %s \n", ft_strnstr(param_1, param_2, 5));
return 0;
}*/