-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
77 lines (70 loc) · 1.89 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
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ogenc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/27 15:10:48 by ogenc #+# #+# */
/* Updated: 2022/12/28 07:26:01 by ogenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_countchar(const char *s, char c, int opt_delimit)
{
size_t i;
i = 0;
if (opt_delimit)
{
while (s[i] && s[i] != c)
i++;
}
else
{
while (s[i] && s[i] == c)
i++;
}
return (i);
}
static size_t ft_wordcounter(const char *str, char c)
{
size_t i;
size_t wordcount;
i = 0;
wordcount = 0;
while (str[i] != '\0' && str[i] == c)
i++;
while (str[i] != '\0')
{
while (str[i] != '\0' && str[i] != c)
i++;
wordcount++;
while (str[i] != '\0' && str[i] == c)
i++;
}
return (wordcount);
}
char **ft_split(char const *s, char c)
{
char **array;
size_t wordcount;
size_t i;
if (!s)
return (NULL);
i = 0;
wordcount = ft_wordcounter(s, c);
array = (char **)malloc(sizeof(char *) * (wordcount + 1));
if (!array)
return (NULL);
while (i < wordcount)
{
s += ft_countchar(s, c, 0);
array[i] = ft_substr(s, 0, ft_countchar(s, c, 1));
if (!array)
return (NULL);
s += ft_countchar(s, c, 1) + ft_countchar(s, c, 0);
i++;
}
array[i] = NULL;
return (array);
}