Files
mmoat/lib/libft/srcs/str/ft_strnstr.c
Loic Deridder 0ec78ee34c builtins
2025-01-15 13:41:40 +01:00

62 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 12:45:20 by nalebrun #+# #+# */
/* Updated: 2024/11/25 16:16:35 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
int j;
i = 0;
if (!haystack || !needle || *needle == 0)
return ((char *)haystack);
while (haystack[i] && i < len)
{
if (haystack[i] == needle[0])
{
j = 0;
while (haystack[i + j] == needle[j] && i + j < len)
{
j++;
if (needle[j] == 0)
return ((char *)(haystack + i));
}
}
i++;
}
return (0);
}
// char *ft_strnstr(const char *haystack, const char *needle, size_t len)
// {
// size_t i;
// size_t j;
//
// i = 0;
// if (!haystack || !needle)
// return (NULL);
// if (needle[0] == 0)
// return ((char *)haystack);
// while (haystack[i] && i < len)
// {
// j = 0;
// while (haystack[i + j] == needle[j] && i + j < len)
// {
// j++;
// if (needle[j] == 0)
// return ((char *)(haystack + i));
// }
// i++;
// }
// return (NULL);
// }