42_libft/ft_strndup.c
Camille Chauvet be438f7dec v2
2022-10-04 14:21:55 +02:00

32 lines
1.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:03:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strndup(const char *s, size_t n)
{
char *out;
size_t i;
out = malloc((n + 1) * sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (i < n && *s)
{
out[i] = s[i];
i++;
}
out[i] = 0;
return (out);
}