-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
85 lines (77 loc) · 1.96 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
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Aamjahed <aamjahed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/01 01:14:00 by Aamjahed #+# #+# */
/* Updated: 2023/10/01 01:14:24 by Aamjahed ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_words(const char *s, char c)
{
int words;
int in_word;
int i;
words = 0;
in_word = 0;
i = 0;
while (s[i] != '\0')
{
if (!in_word && s[i] != c)
{
in_word = 1;
++words;
}
else if (in_word && s[i] == c)
in_word = 0;
++i;
}
return (words);
}
static void advance_index_len(char const *s, char c, int *j, int *len)
{
*len = 0;
while (s[*j] != '\0' && s[*j] == c)
*j = *j + 1;
while (s[*j] != '\0' && s[*j] != c)
{
*j = *j + 1;
*len = *len + 1;
}
}
static void *free_everything(char **res, int stop)
{
int i;
i = 0;
while (i <= stop)
free(res[i++]);
free(res);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **res;
int word_count;
int len;
int i;
int j;
word_count = count_words(s, c);
res = malloc(sizeof(char *) * (word_count + 1));
if (res == NULL)
return (NULL);
i = 0;
j = 0;
while (i < word_count)
{
advance_index_len(s, c, &j, &len);
res[i] = ft_substr(s, j - len, len);
if (res[i] == NULL)
return (free_everything(res, i), NULL);
++i;
}
res[word_count] = NULL;
return (res);
}