-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
executable file
·87 lines (78 loc) · 2.02 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
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_strsplit.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: brobicho <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2017/12/04 03:20:00 by brobicho #+# ## ## #+# */
/* Updated: 2017/12/04 03:20:00 by brobicho ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbr_words(const char *str, char c)
{
int wcount;
int i;
wcount = 0;
i = 0;
if (str[0] == '\0')
return (0);
while (str[i] != '\0')
{
while (str[i] == c)
i++;
if (str[i] >= 33 && str[i] <= 126)
wcount++;
while (str[i] != c && str[i])
i++;
}
return (wcount);
}
static int ft_lcount(const char *str, char c)
{
int nb;
nb = 0;
while (str[nb] != c && str[nb])
nb++;
return (nb);
}
static void ft_ezcpy(const char *src, char *dest, int len)
{
int i;
i = 0;
while (i < len)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
char **ft_strsplit(char const *s, char c)
{
int wcount;
char **res;
int i;
int j;
int lcount;
i = 0;
j = 0;
if (s)
wcount = ft_nbr_words(s, c);
if (!s || !(res = (char**)malloc(sizeof(char*) * wcount + 1)))
return (NULL);
while (j < wcount)
{
while (s[i] == c)
i++;
lcount = ft_lcount(s + i, c);
if (!(res[j] = (char *)malloc(sizeof(*res) * (lcount + 1))))
return (NULL);
ft_ezcpy(s + i, res[j], lcount);
i = i + lcount;
j++;
}
res[j] = 0;
return (res);
}