42_C_PISCINE/C/c07v3/ex05/ft_split.c
Camille Chauvet 29ed24d567 init
2023-05-17 16:45:25 +00:00

72 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/01 16:20:17 by cchauvet #+# #+# */
/* Updated: 2022/08/01 16:20:49 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int ft_strstr(char *str, char *to_find)
{
unsigned int i;
unsigned int j;
i = 0;
while (str[i] != 0)
{
j = 0;
while (str[i + j] == to_find[j])
{
if (to_find[j + 1] == 0)
return (i);
j++;
}
i++;
}
return (i);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != 0)
i++;
return (i);
}
char **ft_split(char *str, char *sep)
{
char **tab;
int i;
int j;
int k;
i = 0;
j = 0;
tab = malloc(sizeof(*str) * ft_strlen(str));
while (str[i] != 0 && ft_strlen(str) > i)
{
k = i;
tab[j] = malloc(sizeof(**tab) * (i - k + ft_strstr(&str[i], sep) + 1));
while (i < k + ft_strstr(&str[k], sep))
{
tab[j][i - k] = str[i];
i++;
}
tab[j][i - k] = '\0';
i = k + ft_strstr(&str[k], sep) + ft_strlen(sep);
j++;
}
tab[j] = malloc(sizeof(char));
tab[j] = "";
return (tab);
}