-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
98 lines (89 loc) · 2.13 KB
/
ft_split.c
File metadata and controls
98 lines (89 loc) · 2.13 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
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 13:40:35 by sanmetol #+# #+# */
/* Updated: 2023/05/19 16:58:13 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_word_count(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != '\0')
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static char *get_words(char const *s, char c, int *start)
{
int end;
char *word;
int i;
while (s[*start] == c)
(*start)++;
end = *start;
while (s[end] && s[end] != c)
end++;
word = (char *)malloc(sizeof(char) * (end - *start + 1));
if (!word)
return (NULL);
i = 0;
while (*start < end)
word[i++] = s[(*start)++];
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
char **splited;
int i;
int start;
int n_words;
n_words = get_word_count(s, c);
splited = (char **)malloc(sizeof(char *) * (n_words + 1));
if (!splited)
return (NULL);
i = 0;
start = 0;
while (i < n_words)
{
splited[i] = get_words(s, c, &start);
if (!splited[i])
{
while (i > 0)
free(splited[--i]);
free(splited);
return (NULL);
}
i++;
}
splited[i] = NULL;
return (splited);
}
/* int main(void)
{
char **splited;
int i;
splited = ft_split(" hola que tal", ' ');
i = 0;
while (splited[i])
{
printf("%s\n", splited[i]);
free(splited[i]);
i++;
}
free(splited);
} */