diff options
Diffstat (limited to 'libft/ft_substr.c')
-rw-r--r-- | libft/ft_substr.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..4b9196a --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 06:35:57 by kdx #+# #+# */ +/* Updated: 2022/09/28 07:27:31 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) + ptr[size] = s[start + size]; + return (ptr); +} |