-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
executable file
·93 lines (84 loc) · 1.88 KB
/
ft_strsplit.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
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jpinyot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/15 19:15:12 by jpinyot #+# #+# */
/* Updated: 2017/11/17 04:55:50 by jpinyot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int len_c(const char *s, char c)
{
size_t i;
size_t cnt;
i = 0;
cnt = 0;
while (s[i])
{
if (s[i] != c)
{
cnt++;
while (s[i] != c && s[i])
i++;
}
else
i++;
}
return (cnt);
}
static int ft_le(const char *s, char c)
{
int i;
int cnt;
i = 0;
cnt = 0;
while (s[i] == c)
i++;
while (s[i] != c && s[i])
{
cnt++;
i++;
}
return (cnt);
}
static char **cpy_ft(char **str, const char *s, char c)
{
int i;
size_t k;
i = 0;
k = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != c && s[i])
{
str[k] = ft_strsub(s, i, ft_le(&s[i], c));
i += ft_le(&s[i], c);
}
while (s[i] == c && s[i])
i++;
k++;
}
str[k] = NULL;
return (str);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
if (s == NULL)
return (NULL);
if (!(str = (char **)malloc(sizeof(char *) * (len_c(s, c) + 1))))
return (NULL);
if (len_c(s, c) == 0)
{
*str = 0;
return (str);
}
str = cpy_ft(str, s, c);
return (str);
}