-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
79 lines (71 loc) · 1.8 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
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyalexan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 10:27:29 by kyalexan #+# #+# */
/* Updated: 2021/12/12 12:31:41 by kyalexan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_words(const char *str, char c)
{
int i;
int not_c;
i = 0;
not_c = 0;
while (*str)
{
if (*str != c && not_c == 0)
{
not_c = 1;
i++;
}
else if (*str == c)
not_c = 0;
str++;
}
return (i);
}
static int count_word_len(const char *str, char c)
{
int i;
i = 0;
while (str[i] != c && str[i])
i++;
return (i);
}
static void *free_all(char **tab, int i)
{
while (i--)
free(tab[i]);
free(tab);
return (NULL);
}
char **ft_split(char const *s, char c)
{
int words;
char **tab;
int i;
if (!s)
return (NULL);
words = count_words(s, c);
tab = (char **) malloc((words + 1) * sizeof(char *));
if (!tab)
return (NULL);
i = 0;
while (words--)
{
while (*s == c && *s != '\0')
s++;
tab[i] = ft_substr((char *)s, 0, count_word_len((char *)s, c));
if (!tab[i])
return (free_all(tab, i));
s = s + count_word_len((char *)s, c);
i++;
}
tab[i] = NULL;
return (tab);
}