-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
56 lines (48 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edetoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:20:52 by edetoh #+# #+# */
/* Updated: 2024/10/25 11:19:11 by edetoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* ft_strnstr searches for 'little' in 'big' up to 'len' characters.
* Takes 'big' (main string) and 'little' (substring).
* Returns a pointer to the beginning of 'little' in 'big', or NULL.
*/
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (little[0] == '\0')
return ((char *)big);
while (big[i])
{
j = 0;
while (big[i + j] && big[i + j] == little[j] && (i + j) < len)
{
j++;
}
if (!little[j])
return ((char *)big + i);
i++;
}
return (0);
}
// #include <string.h>
// #include <stdio.h>
// #include <unistd.h>
// int main(void)
// {
// char *res = strnstr("MZIRIBMZIRIBMZE123", "MZIRIBMZE", 19);
// printf("VRAI %s\n", res);
// char *res2 = ft_strnstr("MZIRIBMZIRIBMZE123", "MZIRIBMZE", 19);
// printf("FAKE %s\n", res2);
// return (1);
// }