From f24566d83807d180c9ce120ce14b1516d148ae38 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 11 Nov 2023 11:20:15 +0100 Subject: [PATCH] split --- Makefile | 5 +++-- ft_split.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- libft.h | 3 ++- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 89ba9c2..a51d80c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2023/11/11 01:04:23 by adjoly ### ########.fr # +# Updated: 2023/11/11 11:12:02 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -45,10 +45,11 @@ SRCS = ft_atoi.c \ ft_strnstr.c \ ft_putendl_fd.c \ ft_strtrim.c \ + ft_split.c \ OBJS = $(SRCS:.c=.o) -FLAGS = -Werror -Wall -Wextra +FLAGS = -g -Werror -Wall -Wextra HEADER = libft.h diff --git a/ft_split.c b/ft_split.c index f293a3a..ccd0e04 100644 --- a/ft_split.c +++ b/ft_split.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 19:04:42 by adjoly #+# #+# */ -/* Updated: 2023/11/11 00:36:54 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 11:15:37 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,31 +28,67 @@ static int ft_countword(char const *s, char sep) return (w_count); } -const int ft_countletter(char const *s, char sep) +static int ft_countletter(char const *s, char sep) { int i; i = 0; - while (s[i] != sep || s[i] != '\0') + while (s[i] != sep && s[i] != '\0') i++; return (i); } +static void *ft_freearr(char **arr) +{ + int i; + + i = 0; + free(arr[i]); + while (arr[i]) + { + i++; + free(arr[i]); + } + free(arr); + return (NULL); +} + char **ft_split(char const *s, char c) { int w_count; char **result; + int i; + int j; + int k; + i = 0; + j = 0; + if (s == NULL) + return (NULL); w_count = ft_countword(s, c); result = malloc((w_count + 1) * sizeof(char *)); if (result == NULL) - return (NULL); - while (s[i]) + return (ft_freearr(result)); + while (i < w_count) { - while () + while (s[j] == c) + j++; + if (s[j] != '\0' && s[j] != c) { - + k = 0; + result[i] = malloc((ft_countletter(&s[j], c) + 1) * sizeof(char)); + if (result == NULL) + return (ft_freearr(result)); + while (s[j] != c) + { + result[i][k] = s[j]; + j++; + k++; + } + result[i][k] = '\0'; } + i++; } + result[i] = NULL; return (result); } diff --git a/libft.h b/libft.h index 69a0b4a..540b4d6 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ -/* Updated: 2023/11/11 01:05:15 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 10:40:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,5 +50,6 @@ char *ft_strrchr(const char *s, int c); char *ft_strnstr(const char *big, const char *little, size_t len); void ft_putendl_fd(char *s, int fd); char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); #endif \ No newline at end of file