From 217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 15 Jul 2019 10:20:37 +0200 Subject: c08 strdup malloc + 1 for \0, c09 begin --- c09/ex02/ft_split.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 c09/ex02/ft_split.c (limited to 'c09/ex02') diff --git a/c09/ex02/ft_split.c b/c09/ex02/ft_split.c new file mode 100644 index 0000000..e1c0186 --- /dev/null +++ b/c09/ex02/ft_split.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ +/* Updated: 2019/07/15 09:21:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int in_charset(char character, char *charset) +{ + while (*charset) + if (character == *charset++) + return (1); + return (0); +} + +int count_segment(char *str, char *charset) +{ + int counter; + + counter = 0; + while (*str) + { + if (!in_charset(*str, charset)) + { + counter++; + while (!in_charset(*str, charset) && *str) + str++; + if (!*str) + break ; + } + str++; + } + return (counter); +} + +char **heck(char *str, char *charset, char *tmp, int i) +{ + char **strs; + int j; + + if ((strs = (char**)malloc(sizeof(char*) + * (count_segment(str, charset) + 1))) == NULL) + return (NULL); + j = 0; + while (*str) + { + while (in_charset(*str, charset)) + str++; + i = 0; + while (!in_charset(str[i], charset) && str[i]) + i++; + if ((tmp = (char*)malloc(sizeof(char) * i + 1)) == NULL) + return (NULL); + i = 0; + while (!in_charset(*str, charset) && *str) + tmp[i++] = *str++; + tmp[i] = '\0'; + strs[j++] = tmp; + } + strs[j] = 0; + return (strs); +} + +char **ft_split(char *str, char *charset) +{ + if (!*str) + return (NULL); + return (heck(str, charset, NULL, 0)); +} -- cgit