libft/ft_strnstr.c
2023-12-12 17:30:46 +01:00

42 lines
1.4 KiB
C
Executable File

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 14:31:40 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:08:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
i = 0;
if (needle == haystack)
return ((char *)haystack);
if (!len && !haystack)
return (0);
while (haystack[i] != '\0')
{
j = 0;
while (haystack[i + j] == needle[j] && (i + j) < len)
{
if (haystack[i + j] == '\0' && needle[j] == '\0')
{
return ((char *)&haystack[j]);
}
j++;
}
if (needle[j] == '\0')
return ((char *)(haystack + i));
i++;
}
return (NULL);
}