From 8adeef1b50a3a819cf6525af94f1fbce62465a7e Mon Sep 17 00:00:00 2001 From: Cabergs Charles Date: Mon, 7 Oct 2019 16:33:51 +0200 Subject: Added calloc and strlcpy, rename out of date --- ft_split.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 ft_split.c (limited to 'ft_split.c') diff --git a/ft_split.c b/ft_split.c new file mode 100644 index 0000000..e3b312c --- /dev/null +++ b/ft_split.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:22:09 by cacharle #+# #+# */ +/* Updated: 2019/10/07 14:30:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static size_t count_segment(char const *s, char c) +{ + size_t counter; + + counter = 0; + while (*s) + { + if (*s == c) + { + s++; + continue ; + } + counter++; + while (*s && *s != c) + s++; + } + return (counter); +} + +static 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) + { + clone[i] = s1[i]; + i++; + } + clone[i] = '\0'; + return (clone); +} + +char **ft_split(char const *s, char c) +{ + char **strs; + size_t size; + size_t i; + size_t j; + + size = count_segment(s, c); + if ((strs = (char**)malloc(sizeof(char*) * (size + 1))) == NULL) + return (NULL); + j = 0; + while (*s) + { + if (*s == c) + { + s++; + continue ; + } + i = 0; + while (s[i] && s[i] != c) + i++; + strs[j++] = ft_strndup(s, i); + s += i; + } + strs[j] = 0; + return (strs); +} -- cgit