42_libft/ft_strnstr.c

36 lines
1.3 KiB
C
Raw Normal View History

2022-09-27 10:29:23 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_strnstr.c :+: :+: :+: */
2022-09-27 10:29:23 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 13:39:33 by cchauvet #+# #+# */
2022-10-04 08:21:55 -04:00
/* Updated: 2022/09/29 21:10:21 by cchauvet ### ########.fr */
2022-09-27 10:29:23 -04:00
/* */
/* ************************************************************************** */
2022-10-04 08:21:55 -04:00
#include "libft.h"
2022-09-27 10:29:23 -04:00
2022-10-04 08:21:55 -04:00
char *ft_strnstr(const char *big, const char *little, size_t len)
2022-09-27 10:29:23 -04:00
{
2022-10-04 08:21:55 -04:00
size_t i;
size_t j;
2022-09-27 10:29:23 -04:00
2022-10-04 08:21:55 -04:00
if (!*little || (little == big && ft_strlen(little) <= len))
return ((char *) big);
i = 0;
while (i < len && big[i])
2022-09-27 10:29:23 -04:00
{
2022-10-04 08:21:55 -04:00
j = 0;
while (big[i + j] == little[j] && i + j < len)
{
if (!little[j + 1])
return ((char *) big + i);
j++;
}
i++;
2022-09-27 10:29:23 -04:00
}
return (NULL);
}