-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
executable file
·85 lines (78 loc) · 1.94 KB
/
ft_strsplit.c
File metadata and controls
executable file
·85 lines (78 loc) · 1.94 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
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: luperez <luperez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/15 22:12:00 by luperez #+# #+# */
/* Updated: 2014/11/19 11:03:48 by luperez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int prepare(char const *s, char *str, char c)
{
int i;
int z;
i = -1;
z = 0;
while (s[++i])
{
if (s[i] == c)
{
if (i > 0 && str[i - 1] != '\0')
{
++z;
str[i] = '\0';
}
else if (i == 0)
str[i] = '\0';
}
else
str[i] = s[i];
}
if (z == 0 && s[0] != c && s[0] != '\0')
z = 1;
return (z);
}
static char **execute(int z, char *str)
{
int i;
char *hook;
char **tabinator;
tabinator = (char **)malloc(sizeof(char) * (z + 1));
if (tabinator == NULL)
return (NULL);
if (z < 1)
{
free(str);
tabinator[0] = NULL;
return (tabinator);
}
i = -1;
hook = str;
while (++i < z)
{
str = ft_next_str(str);
tabinator[i] = str;
str = ft_end_str(str);
}
tabinator[i] = NULL;
free(hook);
return (tabinator);
}
char **ft_strsplit(char const *s, char c)
{
int z;
size_t len;
char *str;
if (s == NULL)
return (NULL);
len = ft_strlen(s) + 1;
str = (char *)malloc(sizeof(char) * ft_strlen(s));
if (str == NULL)
return (NULL);
ft_bzero(str, len);
z = prepare(s, str, c);
return (execute(z, str));
}