split
This commit is contained in:
5
Makefile
5
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
|
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/01 11:03:22 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_strnstr.c \
|
||||||
ft_putendl_fd.c \
|
ft_putendl_fd.c \
|
||||||
ft_strtrim.c \
|
ft_strtrim.c \
|
||||||
|
ft_split.c \
|
||||||
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
FLAGS = -Werror -Wall -Wextra
|
FLAGS = -g -Werror -Wall -Wextra
|
||||||
|
|
||||||
HEADER = libft.h
|
HEADER = libft.h
|
||||||
|
|
||||||
|
50
ft_split.c
50
ft_split.c
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/09 19:04:42 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);
|
return (w_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int ft_countletter(char const *s, char sep)
|
static int ft_countletter(char const *s, char sep)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[i] != sep || s[i] != '\0')
|
while (s[i] != sep && s[i] != '\0')
|
||||||
i++;
|
i++;
|
||||||
return (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)
|
char **ft_split(char const *s, char c)
|
||||||
{
|
{
|
||||||
int w_count;
|
int w_count;
|
||||||
char **result;
|
char **result;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int k;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
if (s == NULL)
|
||||||
|
return (NULL);
|
||||||
w_count = ft_countword(s, c);
|
w_count = ft_countword(s, c);
|
||||||
result = malloc((w_count + 1) * sizeof(char *));
|
result = malloc((w_count + 1) * sizeof(char *));
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return (NULL);
|
return (ft_freearr(result));
|
||||||
while (s[i])
|
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);
|
return (result);
|
||||||
}
|
}
|
||||||
|
3
libft.h
3
libft.h
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/01 10:06:03 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);
|
char *ft_strnstr(const char *big, const char *little, size_t len);
|
||||||
void ft_putendl_fd(char *s, int fd);
|
void ft_putendl_fd(char *s, int fd);
|
||||||
char *ft_strtrim(char const *s1, char const *set);
|
char *ft_strtrim(char const *s1, char const *set);
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user