ft_strnstr - gabrielcoelhodacunha-old/42sp-libft GitHub Wiki

Description

Searches len characters to locate the first occurrence of little in big.

Implementation

char	*ft_strnstr(const char *big, const char *little, size_t len)
{
	size_t	l_len;

	l_len = ft_strlen(little);
	if (!l_len)
		return ((char *) big);
	// Searches for `little` in `big`
	while (*big && len > l_len && ft_strncmp(big, little, l_len))
	{
		len--;
		big++;
	}
	// Checks if `little` was found
	if (len >= l_len && !ft_strncmp(big, little, l_len))
		return ((char *) big);
	return (NULL);
}

References