-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
88 lines (81 loc) · 1.94 KB
/
Copy pathft_split.c
File metadata and controls
88 lines (81 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dramos-h <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 13:10:02 by dramos-h #+# #+# */
/* Updated: 2022/12/28 13:10:05 by dramos-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_mallocsize(char const *s, char c)
{
char *aux;
char **aux2;
int fil;
fil = 0;
aux = (char*)s;
while (*aux)
{
while (*aux == c)
aux++;
if (*aux != '\0')
fil++;
while (*aux != c && *aux)
aux++;
}
aux2 = (char**)malloc((fil + 1) * sizeof(char*));
if (aux2 == NULL)
return (NULL);
return (aux2);
}
static char **ft_copy(const char *s, char c)
{
char *aux;
size_t len;
char **tab;
int i;
i = 0;
len = 0;
aux = (char*)s;
tab = ft_mallocsize(s, c);
if (tab == NULL)
return (NULL);
while (*s)
{
while (*s == c)
s++;
if (*s != '\0')
{
aux = (char*)s;
while (*s != c && *s)
{
s++;
len++;
}
tab[i++] = ft_substr(aux, 0, len);
}
len = 0;
}
tab[i] = NULL;
return (tab);
}
char **ft_split(char const *s, char c)
{
char **tab;
if (!s)
return (NULL);
tab = ft_mallocsize(s, c);
if (tab == NULL)
return (NULL);
tab = ft_copy(s, c);
return (tab);
}
/* int main()
{
const char *s = " split this for me ! ";
char c = ' ';
printf("%s\n", *ft_split( s, c));
} */