-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
119 lines (108 loc) · 2.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gusgonza <gusgonza@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 11:58:26 by gusgonza #+# #+# */
/* Updated: 2024/02/15 19:39:48 by gusgonza ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_free(char **split)
{
size_t pos;
pos = 0;
while (split[pos] != NULL)
{
free(split[pos]);
pos++;
}
free(split);
return (NULL);
}
static size_t count_word(const char *str, char delimit)
{
size_t counter;
size_t index;
if (!str)
return (0);
counter = 0;
index = 0;
while (str[index] != '\0')
{
if (str[index] == delimit)
index++;
else
{
counter++;
while (str[index] != delimit && str[index] != '\0')
index++;
}
}
return (counter);
}
static char *get_word(char const *str, char delimit, size_t *str_pos)
{
char *word;
size_t w_pos;
w_pos = 0;
while (str[*str_pos + w_pos] != '\0' && str[*str_pos + w_pos] != delimit)
w_pos++;
word = (char *)malloc(sizeof(char) * (w_pos + 1));
if (word == NULL)
return (NULL);
w_pos = 0;
while (str[*str_pos] != '\0' && str[*str_pos] != delimit)
{
word[w_pos] = str[*str_pos];
w_pos++;
(*str_pos)++;
}
word[w_pos] = '\0';
return (word);
}
char **ft_split(char const *s, char delimit)
{
char **arr;
size_t pos;
size_t str_pos;
if (!s)
return (NULL);
arr = (char **)malloc((count_word(s, delimit) + 1) * sizeof(char *));
if (!arr)
return (NULL);
pos = 0;
str_pos = 0;
while (s[str_pos] != '\0')
{
if (s[str_pos] == delimit)
str_pos++;
else
{
arr[pos] = get_word(s, delimit, &str_pos);
if (arr[pos] == NULL)
return (ft_free(arr));
pos++;
}
}
arr[pos] = NULL;
return (arr);
}
/* // For testing
#include <stdio.h>
int main(void)
{
char *str2 = " a b c d ";
char delimit = ' ';
char **split = ft_split(str2, delimit);
if (!split)
return (1);
printf("result: %lu\n", count_word(str2, delimit));
for (int i = 0; split[i]; i++)
printf("%s\n", split[i]);
ft_free(split);
return (0);
}
*/