-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
89 lines (80 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: blavonne <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/16 19:45:58 by blavonne #+# #+# */
/* Updated: 2019/09/18 19:00:51 by blavonne ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(const char *tmp, char c)
{
size_t i;
int count;
count = 0;
i = 0;
while (tmp[i])
{
if (tmp[i] != c && (tmp[i + 1] == c || tmp[i + 1] == '\0'))
count++;
i++;
}
return (count);
}
static int ft_push_in_arr(char **arr, char *str, char c, int words)
{
char *word;
char *end;
int begin;
int k;
k = 0;
while (words--)
{
while (str[k] && str[k] == c)
k++;
begin = k;
while (str[k] && str[k] != c)
k++;
end = &str[k];
if (!(word = ft_strsub(str, begin, end - &str[begin])))
return (0);
if (!(*arr++ = ft_strdup(word)))
return (0);
free(word);
}
return (1);
}
static void ft_clear_arr(char **arr)
{
size_t i;
if (!arr)
return ;
i = 0;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
}
char **ft_strsplit(char const *s, char c)
{
char **buf;
size_t words;
if (!s)
return (NULL);
words = ft_count_words((char *)s, c);
if (!(buf = (char **)malloc(sizeof(char *) * (words + 1))))
return (NULL);
if (ft_push_in_arr(buf, (char *)s, c, words))
buf[words] = NULL;
else
{
ft_clear_arr(buf);
return (NULL);
}
return (buf);
}