-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split copy.c
88 lines (77 loc) · 1.9 KB
/
ft_split copy.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: snocita <snocita@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 15:31:37 by snocita #+# #+# */
/* Updated: 2023/01/12 11:11:45 by snocita ### ########.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 == sep || 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);
}
// int main(void)
// {
// char **tabstr;
// int i;
// i = 0;
// tabstr = ft_split(NULL, '_');
// while (tabstr[i] != NULL)
// {
// printf("%d = %s\n", i, tabstr[i]);
// i++;
// }
// return (0);
// }