-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
81 lines (72 loc) · 1.87 KB
/
Copy pathft_split.c
File metadata and controls
81 lines (72 loc) · 1.87 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: haksu <haksu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/25 18:28:09 by haksu #+# #+# */
/* Updated: 2022/12/25 18:28:09 by haksu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_numofcol(char const *s, char c)
{
size_t wc;
size_t i;
wc = 0;
i = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i])
wc++;
while (s[i] != c && s[i])
i++;
}
return (wc);
}
size_t ft_sizeofline(char const *s, char c)
{
size_t i;
i = 0;
while (s[i] && s[i] != c)
i++;
return (i);
}
void ft_ww(char **list, char const *s, size_t wc, char c)
{
size_t i;
size_t j;
size_t w_i;
size_t wl;
i = 0;
w_i = 0;
while (w_i < wc)
{
while (s[i] == c)
i++;
wl = ft_sizeofline(s + i, c);
list[w_i] = (char *)malloc(wl + 1);
j = 0;
while (j < wl)
list[w_i][j++] = s[i++];
list[w_i][j] = 0;
w_i++;
}
list[w_i] = 0;
}
char **ft_split(char const *s, char c)
{
size_t wc;
char **list;
if (!s)
return (0);
wc = ft_numofcol(s, c);
list = (char **)malloc(sizeof(char *) * (wc + 1));
if (!list)
return (0);
ft_ww(list, s, wc, c);
return (list);
}