-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
76 lines (69 loc) · 1.79 KB
/
ft_split.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahjadani <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/06 09:34:29 by ahjadani #+# #+# */
/* Updated: 2021/11/11 22:55:41 by ahjadani ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_skip(char const *s, int pos, char c)
{
while (s[pos] == c)
pos++;
return (pos);
}
static char **ft_clean(char **strs, int pos)
{
while (--pos >= 0)
free(strs[pos]);
free(strs);
return (NULL);
}
static int ft_words_counter(char const *s, char c)
{
unsigned int i;
int count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && (s[i] != c))
i++;
}
return (count);
}
char **ft_split(char const *s, char c)
{
size_t i;
size_t j;
size_t k;
char **strs;
if (!s)
return (NULL);
i = 0;
k = 0;
strs = (char **)malloc(sizeof(char *) * ft_words_counter(s, c) + 1);
if (!strs)
return (NULL);
while (s[i])
{
i = ft_skip(s, i, c);
j = i;
while (s[i] && s[i] != c)
i++;
if (i > j)
strs[k++] = ft_strndup(&s[j], i - j);
if (!strs[k - 1])
return (ft_clean(strs, k - 1));
}
strs[k] = NULL;
return (strs);
}