From 8ec5431354bdb582455e8c32758098c5a0fdada2 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 26 Jul 2019 23:17:36 +0200 Subject: exam final --- exam_final/rendu/ft_split/ft_split.c | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 exam_final/rendu/ft_split/ft_split.c (limited to 'exam_final/rendu/ft_split') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 12:35:48 by exam #+# #+# */ +/* Updated: 2019/07/26 13:09:10 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +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); +} -- cgit