-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
47 lines (43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dalcabre <dalcabre@student.42urduliz.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 12:37:44 by dalcabre #+# #+# */
/* Updated: 2024/01/25 13:21:15 by dalcabre ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
char *ft_strnstr(const char *s, const char *sub_s, size_t n)
{
size_t i;
size_t j;
i = 0;
if (sub_s[i] == '\0')
return ((char *)s);
while (i < n && s[i])
{
if (sub_s[0] == s[i])
{
j = 0;
while (sub_s[j] == s[i + j] && (i + j) < n && sub_s[j])
{
j++;
}
if (ft_strlen(sub_s) == j)
return ((char *)(s + i));
}
i++;
}
return (NULL);
}
// int main(void)
// {
// const char *s = "Hello World! hELLoHello";
// const char *sub_s = "ld!";
// printf("%s", ft_strnstr(s, sub_s, 21));
// return (0);
// }