aboutsummaryrefslogtreecommitdiff
path: root/c09/ex02/ft_split.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-15 10:20:37 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-15 10:20:37 +0200
commit217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 (patch)
tree32a8f1f84e0b5f3617988932ccc068d471207208 /c09/ex02/ft_split.c
parent3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (diff)
downloadpiscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.tar.gz
piscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.tar.bz2
piscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.zip
c08 strdup malloc + 1 for \0, c09 begin
Diffstat (limited to 'c09/ex02/ft_split.c')
-rw-r--r--c09/ex02/ft_split.c76
1 files changed, 76 insertions, 0 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */
+/* Updated: 2019/07/15 09:21:14 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+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));
+}