diff options
Diffstat (limited to 'libft/ft_substr.c')
-rw-r--r-- | libft/ft_substr.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..283db6e --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 06:35:57 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:37:58 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + ssize_t size; + char *ptr; + + if (s == NULL) + return (NULL); + size = ft_strlen(s); + size -= start; + if (size < 0) + size = 0; + if ((size_t)size > len) + size = len; + ptr = malloc(size + 1); + if (ptr == NULL) + return (NULL); + ptr[size] = '\0'; + while (size > 0) + { + size -= 1; + ptr[size] = s[start + size]; + } + return (ptr); +} |