-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
98 lines (88 loc) · 2.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edegraev <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 13:30:14 by edegraev #+# #+# */
/* Updated: 2023/11/14 13:30:31 by edegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int nwords(char const *s, char c)
{
int i;
int n;
i = 0;
n = 0;
while (s[i] != 0)
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == 0))
n++;
i++;
}
return (n);
}
static int split_words(char **str, char const *s, char c, int word)
{
int start;
int end;
start = 0;
end = 0;
while (s[end])
{
if (s[end] == c || s[end] == 0)
start = end + 1;
if (s[end] != c && (s[end + 1] == c || s[end + 1] == 0))
{
str[word] = malloc(sizeof(char) * (end - start + 2));
if (!str[word])
{
while (word++)
free(str[word]);
return (0);
}
ft_strlcpy(str[word], (s + start), end - start + 2);
word++;
}
end++;
}
str[word] = 0;
return (1);
}
char **ft_split(char const *s, char c)
{
char **str;
if (!s)
return (NULL);
str = malloc(sizeof(char *) * (nwords(s, c) + 1));
if (!str)
return (NULL);
if (split_words(str, s, c, 0) == 0)
return (NULL);
return (str);
}
// int main(void)
// {
// char *str = "---Hello-Wolrd-How--";
// char charset = '-';
// char **out;
// int i;
// int j;
// out = ft_split(str, charset);
// // print all words
// i = 0;
// while (out[i])
// {
// j = 0;
// while (out[i][j])
// {
// write(1, &out[i][j], 1);
// j++;
// }
// printf("\n");
// i++;
// }
// return (0);
// }