-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
68 lines (60 loc) · 2.17 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gusgonza <gusgonza@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/24 19:44:33 by gusgonza #+# #+# */
/* Updated: 2024/02/11 16:55:16 by gusgonza ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *str, const char *to_find, size_t len)
{
size_t pos_str;
size_t pos_find;
pos_str = 0;
if (*to_find == '\0')
return ((char *)str);
while (str[pos_str] != '\0' && pos_str < len)
{
pos_find = 0;
while (to_find[pos_find] == str[pos_str + pos_find]
&& (pos_str + pos_find) < len)
{
if (to_find[pos_find + 1] == '\0')
return ((char *)&str[pos_str]);
pos_find++;
}
pos_str++;
}
return (NULL);
}
/*#include <stdio.h>
int main(void)
{
const char *str = "Hello, World! This is an example.";
const char *to_find1 = "World";
const char *to_find2 = "Planet";
const char *to_find3 = "example";
// Buscando "World" en la cadena
char *result1 = ft_strnstr(str, to_find1, 30);
if (result1 != NULL)
printf("Resultado 1: %s\n", result1);
else
printf("Resultado 1: NULL\n");
// Buscando "Planet" en la cadena
char *result2 = ft_strnstr(str, to_find2, 30);
if (result2 != NULL)
printf("Resultado 2: %s\n", result2);
else
printf("Resultado 2: NULL\n");
// Buscando "example" en la cadena
char *result3 = ft_strnstr(str, to_find3, 30);
if (result3 != NULL)
printf("Resultado 3: %s\n", result3);
else
printf("Resultado 3: NULL\n");
return 0;
}*/