-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
97 lines (90 loc) · 2.12 KB
/
ft_strsplit.c
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
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_strsplit.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: thperchi <marvin@le-101.fr> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/07/14 18:21:44 by thperchi #+# ## ## #+# */
/* Updated: 2018/10/15 13:13:24 by thperchi ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static char **fill(char *str, char **tab, char c)
{
int x;
int y;
int z;
x = 0;
y = 0;
z = 0;
while ((str[x] == c) && str[x])
x++;
while (str[x])
{
z = 0;
while (str[x] != c && str[x])
{
tab[y][z] = str[x];
x++;
z++;
}
while ((str[x] == c) && str[x])
x++;
tab[y++][z] = '\0';
}
tab[y] = NULL;
return (tab);
}
static char **memmalloc(char *str, char **tab, char c)
{
int x;
int y;
int z;
x = 0;
y = 0;
z = 0;
while ((str[x] == c) && str[x])
x++;
while (str[x])
{
z = 0;
while (str[x] != c && str[x])
{
x++;
z++;
}
while ((str[x] == c) && str[x])
x++;
if (!(tab[y++] = (char*)malloc(sizeof(char) * (z + 1))))
return (NULL);
}
return (tab);
}
char **ft_strsplit(char const *str, char c)
{
int x;
int y;
char **tab;
x = 0;
y = 0;
if (!str)
return (NULL);
while ((str[x] == c) && str[x])
x++;
while (str[x])
{
while (str[x] != c && str[x])
x++;
while ((str[x] == c) && str[x])
x++;
y++;
}
if (!(tab = (char**)malloc(sizeof(char*) * (y + 1))))
return (0);
if (!(tab = memmalloc((char *)str, tab, c)))
return (NULL);
tab = fill((char *)str, tab, c);
return (tab);
}