93 lines
1.8 KiB
C
93 lines
1.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_split.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/03/18 10:29:59 by lclerel- #+# #+# */
|
|
/* Updated: 2026/03/19 10:07:18 by syxpi ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
|
|
int ft_sep(char c, char *charset)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (charset[i])
|
|
{
|
|
if (c == charset[i])
|
|
return (1);
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int ft_words(char *str, char *charset)
|
|
{
|
|
int i;
|
|
int count;
|
|
|
|
count = 0;
|
|
i = 0;
|
|
while (str[i])
|
|
{
|
|
if (ft_sep(str[i], charset) == 0
|
|
&& (i == 0 || ft_sep(str[i - 1], charset) == 1))
|
|
{
|
|
count++;
|
|
}
|
|
i++;
|
|
}
|
|
return (count);
|
|
}
|
|
|
|
void ft_putstr(char *str)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (str[i])
|
|
{
|
|
write(1, &str[i], 1);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
char **ft_split(char *str, char *charset)
|
|
{
|
|
int i;
|
|
int j;
|
|
int len;
|
|
char **res;
|
|
|
|
len = 0;
|
|
i = 0;
|
|
res = malloc(sizeof(char *) * (ft_words(str, charset) + 1));
|
|
if (!res)
|
|
return (NULL);
|
|
j = 0;
|
|
while (str[i])
|
|
{
|
|
while (str[i] && ft_sep(str[i], charset))
|
|
i++;
|
|
res[j] = malloc(sizeof(char) * (len + 1));
|
|
if (!res[j])
|
|
return (NULL);
|
|
//if (str[i]);
|
|
}
|
|
return (res);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
char *res;
|
|
|
|
ft_split("Test=Test=Test=Test", "=");
|
|
ft_putstr(&res);
|
|
}
|