-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
103 lines (92 loc) · 2.22 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
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hshawand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/03 15:45:02 by hshawand #+# #+# */
/* Updated: 2019/04/15 17:33:46 by hshawand ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
static size_t ft_word_count(char const *s, char c)
{
size_t amount;
s = s + 1;
amount = 0;
while (*s && *(s - 1))
{
if (*s == c && *(s - 1) != c)
amount++;
s = s + 1;
}
return (amount + 1);
}
static char *ft_word_alloc(char const *s, char c)
{
size_t size;
char *space;
size = 0;
while (s[size] != c && s[size])
size++;
space = (char *)malloc(sizeof(char) * (size + 2));
return (space);
}
static int ft_arr_form(char **array, size_t word_amount,
char const *s, char c)
{
size_t i;
size_t j;
i = 0;
while (*s == c && *s)
s = s + 1;
while (i < word_amount && *s)
{
j = 0;
array[i] = ft_word_alloc(s, c);
if (!array[i])
return (-1);
while (*s != c && *s)
{
array[i][j] = *s;
s = s + 1;
j++;
}
while (*s == c && *s)
s = s + 1;
array[i][j] = '\0';
i++;
}
array[i] = NULL;
return (0);
}
static void ft_arr_clean(char **array)
{
size_t i;
i = 0;
while (array[i])
{
free(array[i]);
i++;
}
free(array);
}
char **ft_strsplit(char const *s, char c)
{
char **array;
size_t word_amount;
if (!s)
return (0);
word_amount = ft_word_count(s, c);
array = (char **)malloc(sizeof(char *) * (word_amount + 1));
if (!array)
return (NULL);
if (ft_arr_form(array, word_amount, s, c))
{
ft_arr_clean(array);
return (NULL);
}
return (array);
}