-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
86 lines (79 loc) · 1.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vopolonc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/11 12:56:52 by vopolonc #+# #+# */
/* Updated: 2018/11/15 15:25:12 by vopolonc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int btw_symbol(char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (!s[i])
break ;
while (s[i] != c && s[i])
i++;
j++;
}
return (j);
}
static int get_len(char *s, char c, int word)
{
int i;
int j;
i = 0;
j = 0;
while (word > 0)
{
j = 0;
while (s[i] == c && s[i])
i++;
if (!s[i])
break ;
while (s[i] != c && s[i])
{
i++;
j++;
}
word--;
}
return (j);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int j;
int temp;
char **new;
i = 0;
j = 0;
temp = 0;
if (!s)
return (NULL);
if (!(new = (char**)malloc(sizeof(char*) * btw_symbol((char*)s, c) + 1)))
return (NULL);
while (temp < btw_symbol((char*)s, c))
{
j = 0;
new[temp] = (char*)malloc(sizeof(char) *
get_len((char*)s, c, temp + 1) + 1);
while (s[i] == c && s[i])
i++;
while (s[i] != c && s[i])
new[temp][j++] = s[i++];
new[temp++][j] = '\0';
}
new[temp] = 0;
return (new);
}