-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
104 lines (93 loc) · 2.16 KB
/
ft_strsplit.c
File metadata and controls
104 lines (93 loc) · 2.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jquincy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/21 13:14:44 by jquincy #+# #+# */
/* Updated: 2019/09/21 13:58:35 by jquincy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *through(char **str, char c, int i)
{
char *m;
m = NULL;
while (**str)
{
if (i == 0 && **str != c)
{
m = *str;
break ;
}
if (**str != c && *(*str - 1) == c && i - 1 >= 0)
{
m = *str;
break ;
}
*str = *str + 1;
i++;
}
if (*(*str + 1) != '\0')
*str = *str + 1;
return (m);
}
static char *getwrd(char *m, size_t size)
{
char *tmp;
tmp = ft_strdup(m);
m = ft_strnew(size);
m = ft_strncpy(m, tmp, size);
free(tmp);
tmp = NULL;
return (m);
}
static size_t cutwrd(char *m, char c)
{
size_t size;
size = 0;
while (m[size] != '\0' && m[size] != c)
size++;
return (size);
}
static int wrdnb(char const *s, char c)
{
int nbm;
int i;
nbm = 0;
i = 0;
while (s[i])
{
if (i == 0 && s[i] != c)
nbm++;
if (s[i] != c && (i > 0 && s[i - 1] == c))
nbm++;
i++;
}
return (nbm);
}
char **ft_strsplit(char const *s, char c)
{
char **split;
int nbm;
char *str_s;
int i;
size_t size;
if (s == NULL)
return (NULL);
i = 0;
str_s = (char *)s;
nbm = wrdnb(s, c);
if (!(split = (char **)ft_memalloc(sizeof(*split) * (nbm + 1))))
return (NULL);
while (i < nbm)
{
split[i] = through(&str_s, c, i);
size = cutwrd(split[i], c);
split[i] = getwrd(split[i], size);
i++;
}
split[i] = NULL;
return (split);
}