-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
111 lines (102 loc) · 2.25 KB
/
ft_strsplit.c
File metadata and controls
111 lines (102 loc) · 2.25 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akaseris <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/09/11 22:37:57 by akaseris #+# #+# */
/* Updated: 2018/01/23 17:21:14 by akaseris ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static char **ft_allocrow(char **tbl, char *str, char del)
{
int i;
int c;
i = 0;
c = 0;
while (str[i] == del)
i++;
while (str[i] != '\0')
{
i++;
if (str[i] == del)
{
c++;
while (str[i] == del)
i++;
}
}
if (!(str[i - 1] == del))
c++;
if (!(tbl = (char**)malloc(sizeof(*tbl) * (c + 1))))
return (NULL);
tbl[c] = NULL;
return (tbl);
}
static char **ft_alloccol(char **tbl, char *str, char del)
{
int i;
int c;
int k;
i = 0;
k = 0;
c = 0;
while (str[i] == del)
i++;
while (str[i] != '\0')
{
i++;
k++;
if (str[i] == del || str[i] == '\0')
{
if (!(tbl[c] = (char*)malloc(sizeof(**tbl) * (k + 1))))
return (NULL);
while (str[i] == del)
i++;
k = 0;
c++;
}
}
return (tbl);
}
static char **ft_filltbl(char **tbl, char *str, char del)
{
int i;
int c;
int j;
i = 0;
c = 0;
j = 0;
while (str[i] == del)
i++;
while (str[i] != '\0')
{
tbl[c][j] = str[i];
i++;
j++;
if (str[i] == del || str[i] == '\0')
{
tbl[c][j] = '\0';
c++;
j = 0;
while (str[i] == del)
i++;
}
}
return (tbl);
}
char **ft_strsplit(char const *s, char c)
{
char **split;
if (!s || !c)
return (0);
split = NULL;
if (!(split = ft_allocrow(split, (char *)s, c)))
return (0);
if (!(split = ft_alloccol(split, (char *)s, c)))
return (0);
split = ft_filltbl(split, (char *)s, c);
return (split);
}