-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
90 lines (81 loc) · 1.88 KB
/
ft_split.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 10:24:28 by ytaya #+# #+# */
/* Updated: 2021/11/06 19:48:19 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_getnwords(char const *s, char c)
{
int i;
int n;
n = 0;
i = 0;
while (s[i])
{
if (s[i] != c)
{
n++;
while (s[i + 1] && s[i + 1] != c)
i++;
}
i++;
}
return (n);
}
static int ft_getwordsize(const char *s, char c)
{
int len;
len = 0;
while (s[len] && s[len] != c)
len++;
return (len);
}
static char *ft_strucpy(char const *s, char c)
{
int i;
char *dest;
i = 0;
dest = (char *) malloc(sizeof(char) * ft_getwordsize(s, c));
if (!dest)
return (0);
while (s[i] && s[i] != c)
{
dest[i] = s[i];
i++;
}
dest[i] = '\0';
return (dest);
}
char **ft_split(char const *s, char c)
{
char *from;
int i;
int j;
char **table;
i = 0;
j = 0;
if (!s)
return (0);
table = (char **) malloc((sizeof(char *) * ft_getnwords(s, c)) + 1);
if (!table)
return (0);
while (s[i])
{
if (s[i] != c)
{
from = (char *) &s[i];
while (s[i + 1] && s[i + 1] != c)
i++;
table[j++] = ft_strucpy(from, c);
}
i++;
}
table[j] = 0;
return (table);
}