-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
97 lines (86 loc) · 2.13 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lubaujar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/12 14:42:32 by lubaujar #+# #+# */
/* Updated: 2014/11/13 15:00:46 by lubaujar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count_words(char const *s, char c)
{
size_t i;
size_t ret;
int in_word;
ret = 0;
i = 0;
in_word = 0;
while (s[i])
{
if (s[i] == c && in_word)
in_word = !in_word;
else if (s[i] != c && !in_word)
{
ret++;
in_word = !in_word;
}
i++;
}
return (ret);
}
static char **ft_alloc_tab(size_t nb_words)
{
char **to_return;
to_return = (char **)ft_memalloc(sizeof(char *) * (nb_words + 1));
if (to_return == NULL)
return (NULL);
to_return[0] = NULL;
return (to_return);
}
static size_t ft_lenght_word(char const *s, char c)
{
size_t i;
i = 0;
while (s && s[i] != c)
{
i++;
}
return (i);
}
static void ft_split(char **to_return, char const *s, char c)
{
size_t tab;
size_t len_word;
char *word;
tab = 0;
while (*s)
{
len_word = ft_lenght_word(s, c);
if (len_word != 0)
{
word = ft_strsub(s, 0, len_word);
to_return[tab] = word;
tab++;
}
s = s + len_word;
while (*s == c)
s++;
}
to_return[tab] = NULL;
}
char **ft_strsplit(char const *s, char c)
{
char **to_return;
size_t nb_words;
if (s == NULL)
return (NULL);
nb_words = ft_count_words(s, c);
to_return = ft_alloc_tab(nb_words);
if (to_return == NULL)
return (NULL);
ft_split(to_return, s, c);
return (to_return);
}