ft_strsplit: corrected missing last word

This commit is contained in:
Tanguy MAZE 2018-04-08 16:57:26 +02:00
parent 0ef452afdc
commit 4fff8a050c

View File

@ -6,10 +6,11 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/07 17:54:29 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 <stdio.h>
#include "libft.h" #include "libft.h"
static int count_words(char const *s, char c) 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; in_word = 0;
nb_words = 0; nb_words = 0;
i = -1; i = -1;
while (s[++i]) while (++i == 0 || s[i - 1])
if (!in_word && s[i] != c) if (!in_word && s[i] != c && s[i] != '\0')
{ {
in_word = 1; in_word = 1;
nb_words++; nb_words++;
} }
else if (in_word && s[i] == c) else if (in_word && (s[i] == c || s[i] == '\0'))
in_word = 0; in_word = 0;
return (nb_words); return (nb_words);
} }
@ -42,10 +43,10 @@ static char **get_table(char const *s, char c, char **tab)
nb_words = 0; nb_words = 0;
in_word = 0; in_word = 0;
i = -1; i = -1;
while (s[++i]) while (++i == 0 || s[i - 1])
if (!in_word && s[i] != c && (in_word = 1)) if (!in_word && s[i] != c && (in_word = 1))
start = i; 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) if ((tab[nb_words] = ft_strsub(s, start, i - start)) == NULL)
{ {