-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
73 lines (66 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hezzahir <hamza.ezzahiry@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/14 21:25:31 by hezzahir #+# #+# */
/* Updated: 2019/05/14 22:21:53 by hezzahir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(const char *s, char c)
{
int count;
int i;
i = 0;
count = 0;
while (s[i] == c)
i++;
while (s[i])
{
if (s[i] == c && s[i - 1] != c)
count += 1;
if (s[i] != c && s[i + 1] == '\0')
count += 1;
i++;
}
return (count);
}
static int ft_word_len(const char *s, char c)
{
int len;
len = 0;
while (*s != c && *s != '\0')
{
len++;
s++;
}
return (len);
}
char **ft_strsplit(const char *s, char c)
{
char **tab;
int nb_word;
int x;
if (!s)
return (NULL);
x = 0;
nb_word = ft_count_words(s, c);
tab = (char **)malloc(sizeof(*tab) * (nb_word + 1));
if (tab == NULL)
return (NULL);
while (nb_word--)
{
while (*s == c && *s != '\0')
s++;
tab[x] = ft_strsub(s, 0, ft_word_len(s, c));
if (tab[x] == NULL)
return (NULL);
s = s + ft_word_len(s, c);
x++;
}
tab[x] = NULL;
return (tab);
}