From 4fff8a050c8d44d50aa94c2319194eb483edec4e Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Sun, 8 Apr 2018 16:57:26 +0200 Subject: [PATCH] ft_strsplit: corrected missing last word --- ft_strsplit.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ft_strsplit.c b/ft_strsplit.c index ef31c03..8d8d517 100644 --- a/ft_strsplit.c +++ b/ft_strsplit.c @@ -6,10 +6,11 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/07 17:54:29 by tmaze #+# #+# */ -/* Updated: 2018/04/07 18:51:30 by tmaze ### ########.fr */ +/* Updated: 2018/04/08 16:54:13 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include "libft.h" static int count_words(char const *s, char c) @@ -21,13 +22,13 @@ static int count_words(char const *s, char c) in_word = 0; nb_words = 0; i = -1; - while (s[++i]) - if (!in_word && s[i] != c) + while (++i == 0 || s[i - 1]) + if (!in_word && s[i] != c && s[i] != '\0') { in_word = 1; nb_words++; } - else if (in_word && s[i] == c) + else if (in_word && (s[i] == c || s[i] == '\0')) in_word = 0; return (nb_words); } @@ -42,10 +43,10 @@ static char **get_table(char const *s, char c, char **tab) nb_words = 0; in_word = 0; i = -1; - while (s[++i]) + while (++i == 0 || s[i - 1]) if (!in_word && s[i] != c && (in_word = 1)) start = i; - else if (in_word && s[i] == c) + else if (in_word && (s[i] == c || s[i] == '\0')) { if ((tab[nb_words] = ft_strsub(s, start, i - start)) == NULL) {