-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
78 lines (71 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccline <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/06 09:56:02 by ccline #+# #+# */
/* Updated: 2019/10/06 10:48:18 by ccline ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wrds(char const *s, char c)
{
int words;
words = 0;
if (*s && *s != c)
{
s++;
words++;
}
while (*s)
{
while (*s == c)
{
s++;
if (*s && *s != c)
words++;
}
s++;
}
return (words);
}
static int ft_wlen(char const *s, char c)
{
int len;
len = 0;
while (*s && *s != c)
{
len++;
s++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int j;
char **res;
i = 0;
j = 0;
if (!s || (!(res = (char **)malloc(sizeof(char*) * (ft_wrds(s, c) + 1)))))
return (NULL);
while (*s)
{
while (*s && *s == c)
s++;
if (*s && *s != c)
{
if (!(res[i] = (char *)malloc(sizeof(char) * (ft_wlen(s, c) + 1))))
return (NULL);
while (*s && *s != c)
res[i][j++] = (char)*s++;
res[i][j] = '\0';
i++;
j = 0;
}
}
res[i] = NULL;
return (res);
}