libft/str/ft_strnstr.c
Raphael 905ffd4b72
refactor: only the usefull header
- Removing all 'libft.h' mention
- Using the include only on the files needed by
2025-09-01 18:45:33 +02:00

51 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:57:44 by rparodi #+# #+# */
/* Updated: 2025/09/01 17:54:58 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
/**
* @brief Locates a substring within a string, up to a specified length.
*
* The `ft_strnstr` function locates the first occurrence of the null-terminated
* string `needle` within the string `haystack`,
* searching only up to `len` characters.
*
* @param big The string to search.
* @param little The substring to locate.
* @param len The maximum number of characters to search.
*
* @return A pointer to the beginning of the located substring,
* or NULL if `needle` is not found.
*/
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (len == 0 && (!big || !little))
return (NULL);
if (!little[i])
return ((char *)big);
while (big[i] && i < len)
{
j = 0;
while (i + j < len && little[j] == big[i + j])
{
j++;
if (little[j] == '\0')
return ((char *)(big + i));
}
i++;
}
return (0);
}