From 3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 15 Jul 2019 08:15:37 +0200 Subject: c07 passed, c08 in progress, rush01(+ 6x6 try) --- c07/ex05/ft_split.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'c07/ex05/ft_split.c') diff --git a/c07/ex05/ft_split.c b/c07/ex05/ft_split.c index 956782f..3ff2e43 100644 --- a/c07/ex05/ft_split.c +++ b/c07/ex05/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ -/* Updated: 2019/07/09 12:42:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/13 08:15:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,41 +24,51 @@ int count_segment(char *str, char *charset) { int counter; - counter = 1; + counter = 0; while (*str) { - if (in_charset(*str, charset)) + if (!in_charset(*str, charset)) + { counter++; + while (!in_charset(*str, charset) && *str) + str++; + if (!*str) + break ; + } str++; } return (counter); } -char **ft_split(char *str, char *charset) +char **heck(char *str, char *charset, char *tmp, int i) { char **strs; - char *tmp; - int i; int j; - int k; - strs = (char**)malloc(sizeof(char*) * (count_segment(str, charset) + 1)); - printf("%d\n", count_segment(str, charset)); - k = 0; + 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)) + while (!in_charset(str[i], charset) && str[i]) i++; - tmp = (char*)malloc(sizeof(char) * i); - if (tmp == NULL) - printf("bonjour"); + if ((tmp = (char*)malloc(sizeof(char) * i + 1)) == NULL) + return (NULL); i = 0; - while (!in_charset(*str, charset)) + while (!in_charset(*str, charset) && *str) tmp[i++] = *str++; - strs[k++] = tmp; - str++; + tmp[i] = '\0'; + strs[j++] = tmp; } - strs[k] = 0; + strs[j] = 0; return (strs); } + +char **ft_split(char *str, char *charset) +{ + return (heck(str, charset, NULL, 0)); +} -- cgit