Repository navigation
Expand file tree
/
Copy pathft_strnstr.c
More file actions
56 lines (50 loc) · 1.78 KB
/
Copy pathft_strnstr.c
File metadata and controls
56 lines (50 loc) · 1.78 KB
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: smasatak <smasatak@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/30 03:11:59 by smasatak #+# #+# */
/* Updated: 2026/06/18 13:40:49 by smasatak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t b_count;
size_t l_count;
if (!little)
return (NULL);
if (!*little)
return ((char *)big);
if (!big || len == 0)
return (NULL);
b_count = 0;
while (big[b_count] && b_count < len)
{
l_count = 0;
while (b_count + l_count < len && little[l_count]
&& big[b_count + l_count] == little[l_count])
{
l_count++;
if (little[l_count] == '\0')
return ((char *)(big + b_count));
}
b_count++;
}
return (NULL);
}
// #include <stdio.h>
// int main(void)
// {
// const char str1[] = "Hello";
// const char str2[] = "el";
// const char str3[] = "";
// printf("%s\n", ft_strnstr(str1, str2, 4));
// printf("%s\n", ft_strnstr(str1, str3, 4));
// printf("%s\n", ft_strnstr(str1, str2, 0));
// printf("%s\n", ft_strnstr(str1, NULL, 2));
// printf("%s\n", ft_strnstr(NULL, str2, 2));
// return (0);
// }