From 14914f50c3de6c5444e13cf67db064b03c1c90ef Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 16 Jul 2019 12:58:13 +0200 Subject: c09 passed, c10 start, exam00 and exam01 --- exam01/rendu/ft_split/ft_split.c | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 exam01/rendu/ft_split/ft_split.c (limited to 'exam01/rendu/ft_split/ft_split.c') diff --git a/exam01/rendu/ft_split/ft_split.c b/exam01/rendu/ft_split/ft_split.c new file mode 100755 index 0000000..7c8e5c7 --- /dev/null +++ b/exam01/rendu/ft_split/ft_split.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/12 18:35:38 by exam #+# #+# */ +/* Updated: 2019/07/12 19:27:00 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#define ISSEP(c) (c == ' ' || c == '\n' || c == '\t') + +int count_segment(char *str) +{ + int counter; + + counter = 0; + while (*str) + { + if (!ISSEP(*str)) + { + counter++; + while (*str && !ISSEP(*str)) + str++; + if (!*str) + break; + } + str++; + } + return (counter); +} + +char **ft_split(char *str) +{ + char **split; + char *tmp; + int i; + int j; + int segments; + + segments = count_segment(str); + split = (char**)malloc(sizeof(char*) * segments + 1); + if (split == NULL) + return (NULL); + j = 0; + while (j < segments) + { + if (ISSEP(*str)) + { + str++; + continue; + } + i = 0; + while (!ISSEP(str[i])) + i++; + tmp = (char*)malloc(sizeof(char) * i + 1); + if (tmp == NULL) + return (NULL); + i = 0; + while (!ISSEP(*str)) + tmp[i++] = *str++; + tmp[i] = '\0'; + split[j] = tmp; + j++; + } + split[j] = NULL; + return (split); +} -- cgit