-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
71 lines (64 loc) · 1.82 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lboukrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/16 19:19:15 by lboukrou #+# #+# */
/* Updated: 2019/12/21 19:03:03 by lboukrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static char **ft_free_all(char ***tab)
{
int i;
i = 0;
if (!*tab)
return (NULL);
while ((*tab)[i])
{
free((*tab)[i]);
i++;
}
free(*tab);
return (NULL);
}
static size_t ft_words_count(char const *s, char c)
{
size_t words;
size_t i;
i = -1;
words = 0;
while (++i < ft_strlen(s))
words = (s[i] != c && (!i || s[i - 1] == c)) ? words + 1 : words;
return (words);
}
char **ft_strsplit(char const *s, char c)
{
char **tab;
size_t words;
size_t i;
size_t y;
if (s == NULL || !c)
return (NULL);
words = ft_words_count(s, c);
if ((tab = malloc(sizeof(char*) * (words + 1))) == NULL)
return (ft_free_all(&tab));
i = -1;
words = -1;
while (s[++i])
{
if (s[i] != c && (!i || s[i - 1] == c))
y = i;
if (s[i] != c && (s[i + 1] == c || !s[i + 1]))
{
tab[++words] = ft_strsub(s, y, i - y + 1);
if (!tab[words])
return (ft_free_all(&tab));
}
}
tab[words + 1] = NULL;
return (tab);
}