diff options
Diffstat (limited to 'c07/ex00/ft_strdup.c')
| -rw-r--r-- | c07/ex00/ft_strdup.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/c07/ex00/ft_strdup.c b/c07/ex00/ft_strdup.c new file mode 100644 index 0000000..c63bd27 --- /dev/null +++ b/c07/ex00/ft_strdup.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 15:39:50 by cacharle #+# #+# */ +/* Updated: 2019/07/08 07:49:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <errno.h> + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} + +char *ft_strdup(char *src) +{ + int i; + char *dup_ptr; + + dup_ptr = malloc(sizeof(char) * ft_strlen(src)); + if (dup_ptr == NULL) + { + errno = ENOMEM; + return (NULL); + } + i = 0; + while (src[i]) + { + dup_ptr[i] = src[i]; + i++; + } + dup_ptr[i] = '\0'; + return (dup_ptr); +} |
