42_libft/ft_strndup.c

32 lines
1.1 KiB
C
Raw Normal View History

2022-09-27 11:25:41 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_strndup.c :+: :+: :+: */
2022-09-27 11:25:41 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-04 08:21:55 -04:00
/* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:03:02 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-04 08:21:55 -04:00
char *ft_strndup(const char *s, size_t n)
2022-09-27 11:25:41 -04:00
{
2022-10-04 08:21:55 -04:00
char *out;
size_t i;
2022-09-27 11:25:41 -04:00
2022-10-04 08:21:55 -04:00
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);
2022-09-27 11:25:41 -04:00
}