Push the final version
This commit is contained in:
commit
d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions
100
libft/ft_split.c
Executable file
100
libft/ft_split.c
Executable file
|
|
@ -0,0 +1,100 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/18 15:21:57 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int ft_check_space(int c)
|
||||
{
|
||||
if (c == 32 || (c >= 9 && c <= 13))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int count_words(const char *str)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (str[i])
|
||||
{
|
||||
while (ft_check_space(str[i]) == 1 && str[i])
|
||||
i++;
|
||||
if (ft_check_space(str[i]) == 0 && str[i])
|
||||
{
|
||||
while (ft_check_space(str[i]) == 0 && str[i])
|
||||
i++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char *ft_strndup(const char *s, int j)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
str = (char *)malloc((j + 1));
|
||||
if (!str)
|
||||
error_message("alloc strndup", NULL, NULL, NULL);
|
||||
while (s[i] && i < j)
|
||||
{
|
||||
str[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
static char **ext_w(char **words_array, const char *str, int size)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
while (ft_check_space(str[i]) == 1 && str[i])
|
||||
i++;
|
||||
str = str + i;
|
||||
i = 0;
|
||||
while (ft_check_space(str[i]) == 0 && str[i])
|
||||
i++;
|
||||
words_array[j++] = ft_strndup(str, i);
|
||||
str = str + i;
|
||||
i = 0;
|
||||
}
|
||||
words_array[j] = 0;
|
||||
return (words_array);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s)
|
||||
{
|
||||
int size;
|
||||
char **words_array;
|
||||
|
||||
size = count_words(s);
|
||||
words_array = (char **)malloc(sizeof(char *) * (size + 1));
|
||||
if (!words_array)
|
||||
error_message("alloc word cut", NULL, NULL, NULL);
|
||||
if (size == 0)
|
||||
{
|
||||
words_array[0] = NULL;
|
||||
return (words_array);
|
||||
}
|
||||
words_array = ext_w(words_array, s, size);
|
||||
return (words_array);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue