-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
69 lines (62 loc) · 1.66 KB
/
ft_split.c
File metadata and controls
69 lines (62 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarahan <akarahan@student.42istanbul.com. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/03 12:22:04 by akarahan #+# #+# */
/* Updated: 2022/01/03 15:10:49 by akarahan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_allocate(char **tab, char const *s, char sep)
{
char **tab_p;
char const *tmp;
tmp = s;
tab_p = tab;
while (*tmp)
{
while (*s == sep)
++s;
tmp = s;
while (*tmp && *tmp != sep)
++tmp;
if (tmp > s)
{
*tab_p = ft_substr(s, 0, tmp - s);
s = tmp;
++tab_p;
}
}
*tab_p = NULL;
}
static int ft_count_words(char const *s, char sep)
{
int word_count;
word_count = 0;
while (*s)
{
while (*s == sep)
++s;
if (*s)
++word_count;
while (*s && *s != sep)
++s;
}
return (word_count);
}
char **ft_split(char const *s, char c)
{
char **new;
int size;
if (!s)
return (NULL);
size = ft_count_words(s, c);
new = (char **)malloc(sizeof(char *) * (size + 1));
if (!new)
return (NULL);
ft_allocate(new, s, c);
return (new);
}