aboutsummaryrefslogtreecommitdiff
path: root/exam_final/rendu/ft_split/ft_split.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-26 23:17:36 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-26 23:17:36 +0200
commit8ec5431354bdb582455e8c32758098c5a0fdada2 (patch)
tree480c68814f822439850029df0e0249a2cafb8177 /exam_final/rendu/ft_split/ft_split.c
parent475449dd4b1f3308bac6f72c34d87812216a0738 (diff)
downloadpiscine-8ec5431354bdb582455e8c32758098c5a0fdada2.tar.gz
piscine-8ec5431354bdb582455e8c32758098c5a0fdada2.tar.bz2
piscine-8ec5431354bdb582455e8c32758098c5a0fdada2.zip
exam final
Diffstat (limited to 'exam_final/rendu/ft_split/ft_split.c')
-rwxr-xr-xexam_final/rendu/ft_split/ft_split.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/exam_final/rendu/ft_split/ft_split.c b/exam_final/rendu/ft_split/ft_split.c
new file mode 100755
index 0000000..75d9f8d
--- /dev/null
+++ b/exam_final/rendu/ft_split/ft_split.c
@@ -0,0 +1,74 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_split.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/26 12:35:48 by exam #+# #+# */
+/* Updated: 2019/07/26 13:09:10 by exam ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+int in_charset(char c)
+{
+ return (c == ' ' || c == '\n' || c == '\t');
+}
+
+int count_segment(char *str)
+{
+ int counter;
+
+ counter = 0;
+ while (*str)
+ {
+ if (in_charset(*str))
+ {
+ str++;
+ continue ;
+ }
+ counter++;
+ while (*str && !in_charset(*str))
+ str++;
+ }
+ return (counter);
+}
+
+char **ft_split(char *str)
+{
+ char **strs;
+ char *tmp;
+ int size;
+ int i;
+ int j;
+
+ size = count_segment(str);
+ if ((strs = (char**)malloc(sizeof(char*) * (size + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < size)
+ {
+ if (*str && in_charset(*str))
+ {
+ str++;
+ continue ;
+ }
+ j = 0;
+ while (str[j] && !in_charset(str[j]))
+ j++;
+ if ((tmp = (char*)malloc(sizeof(char) * (j + 1))) == NULL)
+ return (NULL);
+ j = 0;
+ while (*str && !in_charset(*str))
+ {
+ tmp[j] = *str++;
+ j++;
+ }
+ tmp[j] = '\0';
+ strs[i++] = tmp;
+ }
+ strs[i] = NULL;
+ return (strs);
+}