-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (77 loc) · 1.84 KB
/
ft_split.c
File metadata and controls
84 lines (77 loc) · 1.84 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
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xvan-ham <xvan-ham@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/22 19:16:16 by xvan-ham #+# #+# */
/* Updated: 2019/11/27 19:21:33 by xvan-ham ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_rows(char const *s, char c)
{
int i;
int j;
int rows;
i = 0;
j = 0;
rows = 0;
while (s[i] && s[i] == c)
i++;
while (s[i])
{
if (s[i] && s[i] != c)
j = 1;
if (s[i] == c)
{
while (s[i] && s[i] == c)
i++;
if (s[i])
rows++;
}
else
i++;
}
return (rows + j);
}
static char *ft_str_malloc(char const *s, char c)
{
int i;
char *str;
i = 0;
while (s[i] && s[i] != c)
i++;
if (!(str = (char *)malloc(sizeof(char) * (i + 1))))
return (NULL);
ft_strlcpy(str, s, i + 1);
return (str);
}
char **ft_split(char const *s, char c)
{
int rows;
int i;
char **tab;
if (!s)
return (NULL);
rows = ft_rows(s, c);
i = -1;
if (!(tab = malloc(sizeof(char *) * (rows + 1))))
return (NULL);
while (++i < rows)
{
while (*s == c)
s++;
if (!(tab[i] = ft_str_malloc(s, c)))
{
while (i > 0)
free(tab[i--]);
free(tab);
return (NULL);
}
s += ft_strlen(tab[i]);
}
tab[i] = 0;
return (tab);
}