-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
85 lines (75 loc) · 1.11 KB
/
ft_split.c
File metadata and controls
85 lines (75 loc) · 1.11 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
#include "libft.h"
char **clear(char **arr)
{
unsigned int i;
i = 0;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
return (NULL);
}
int countstr(char const *s, char c)
{
int i;
int nbstr;
i = 0;
nbstr = 0;
while (s[i] != '\0')
{
while (s[i] != c && s[i] != '\0')
i++;
while (s[i] == c && s[i] != '\0')
i++;
nbstr++;
}
return (nbstr);
}
int countchar(char const *s, int i, char c)
{
int nbchar;
nbchar = 0;
while (s[i] != c && s[i] != '\0')
{
nbchar++;
i++;
}
return (nbchar);
}
char **createarr(char const *s, char c)
{
char **arr;
int i;
int j;
int k;
i = 0;
j = 0;
arr = ft_calloc((countstr(s, c) + 1), sizeof(char *));
if (!arr)
return (0);
while (s[i] != '\0')
{
k = 0;
arr[j] = ft_calloc((countchar(s, i, c) + 1), sizeof(char));
if (!arr[j])
return (clear(arr));
while (s[i] != c && s[i] != '\0')
arr[j][k++] = s[i++];
while (s[i] == c && s[i] != '\0')
i++;
j++;
}
return (arr);
}
char **ft_split(char const *s, char c)
{
char **arr;
s = ft_strtrim(s, &c);
if (!s)
return (0);
arr = createarr(s, c);
free((void *)s);
return (arr);
}