aboutsummaryrefslogtreecommitdiff
path: root/c07/ex05/ft_split.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-15 08:15:37 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-15 08:15:37 +0200
commit3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (patch)
tree25b02c02f5140dbefbabd7720f292d8be3d5cc51 /c07/ex05/ft_split.c
parentc2bf9fcefbb4453cee271ccd1af9674ad2f3a181 (diff)
downloadpiscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.gz
piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.bz2
piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.zip
c07 passed, c08 in progress, rush01(+ 6x6 try)
Diffstat (limited to 'c07/ex05/ft_split.c')
-rw-r--r--c07/ex05/ft_split.c46
1 files changed, 28 insertions, 18 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
+}