-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
93 lines (85 loc) · 2.01 KB
/
ft_split.c
File metadata and controls
93 lines (85 loc) · 2.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abait-ta <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/22 16:05:31 by abait-ta #+# #+# */
/* Updated: 2022/10/22 16:32:19 by abait-ta ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_words(char const *s, char c)
{
int i;
int words;
if (s[0] == '\0')
return (0);
words = 0;
i = 0;
while (s[i] == c)
i++;
while (s[i])
{
if (s[i] == c && s[i - 1] != c)
words++;
i++;
}
if (s[i - 1] != c)
words++;
return (words);
}
static char *next_word(char *str, char c, int *len)
{
int i;
char *tmp;
int count;
i = 0;
count = 0;
while (str[i] && str[i] == c)
i++;
tmp = &str[i];
while (str[i] && str[i] != c)
{
count++;
i++;
}
*len = count;
return (tmp);
}
static char **ft_freetab(char **str, int tofree)
{
while (--tofree >= 0)
free(str[tofree]);
free(str);
str = NULL;
return (str);
}
char **ft_split(char const *s, char c)
{
int i;
char **str;
char *tmp;
int words;
int len;
if (!s)
return (NULL);
i = -1;
words = ft_words(s, c);
str = (char **)malloc(sizeof(char *) * (words + 1));
if (!str)
return (NULL);
tmp = (char *)s;
len = 0;
while (++i < words)
{
tmp = next_word(tmp + len, c, &len);
str[i] = (char *)malloc(len + 1);
if (!str[i])
return (ft_freetab(str, i));
ft_strlcpy(str[i], tmp, len + 1);
}
str[i] = 0;
return (str);
}