-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
102 lines (93 loc) · 2.08 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
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccristia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/25 16:34:30 by ccristia #+# #+# */
/* Updated: 2017/12/01 20:38:46 by ccristia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nr_count(char *s, char c)
{
int i;
int rn;
int stp;
i = 0;
rn = 0;
stp = 0;
while (s[i] != '\0')
{
if (s[i] != c && stp == 0)
{
rn++;
stp++;
}
else if (s[i] == c)
stp = 0;
i++;
}
return (rn);
}
static void ft_colm_malloc(char **ret, char *s, char c)
{
int coln;
int i;
int line;
coln = 0;
i = 0;
line = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
coln++;
if (s[i + 1] == c || s[i + 1] == '\0')
{
ret[line++] = (char *)malloc(sizeof(char) * (coln + 1));
i++;
}
}
if (s[i] == c)
coln = 0;
i++;
}
}
static void ft_isprint_set(char **ret, char *s, char c)
{
int i;
int line;
int coln;
i = 0;
line = 0;
coln = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
ret[line][coln++] = s[i];
if (s[i + 1] == c || s[i + 1] == '\0')
{
ret[line][coln] = '\0';
coln = 0;
line++;
}
}
i++;
}
ret[line] = NULL;
}
char **ft_strsplit(char const *s, char c)
{
char **ret;
if (s == NULL)
return (NULL);
ret = (char **)malloc(sizeof(char *) * (ft_nr_count((char *)s, c) + 1));
if (ret == NULL)
return (NULL);
ft_colm_malloc(ret, (char *)s, c);
ft_isprint_set(ret, (char *)s, c);
return (ret);
}