diff options
Diffstat (limited to 'ft_strndup.c')
| -rw-r--r-- | ft_strndup.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/ft_strndup.c b/ft_strndup.c new file mode 100644 index 0000000..d43c0ce --- /dev/null +++ b/ft_strndup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/25 03:28:52 by cacharle #+# #+# */ +/* Updated: 2019/10/25 03:36:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <string.h> + +char *ft_strndup(const char *s1, size_t n) +{ + char *clone; + size_t i; + + if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL) + return (NULL); + i = 0; + while (i < n && s1[i]) + { + clone[i] = s1[i]; + i++; + } + clone[i] = '\0'; + return (clone); +} |
