-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
112 lines (102 loc) · 2.29 KB
/
Copy pathft_split.c
File metadata and controls
112 lines (102 loc) · 2.29 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaalmoui <yaalmoui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/09/09 13:07:20 by yaalmoui #+# #+# */
/* Updated: 2026/09/10 17:05:41 by yaalmoui ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t count_words(const char *s, char delimeter)
{
size_t words;
int inside_word;
int i;
words = 0;
i = 0;
inside_word = 1;
while (s[i])
{
inside_word = 0;
while (s[i] == delimeter && s[i])
i++;
while (s[i] != delimeter && s[i])
{
if (!inside_word)
{
words++;
inside_word = 1;
}
i++;
}
}
return (words);
}
int safe_malloc(char **word_arr, int position, size_t size)
{
int i;
i = 0;
word_arr[position] = malloc(size);
if (word_arr == NULL)
{
while (i < position)
{
free(word_arr[i]);
i++;
}
free(word_arr);
return (1);
}
return (0);
}
int fill(char **word_arr, char const *s, char c)
{
size_t len;
int position;
position = 0;
while (*s)
{
len = 0;
while (*s == c && *s)
s++;
while (*s != c && *s)
{
len++;
s++;
}
if (len)
{
if (safe_malloc(word_arr, position, len + 1))
return (1);
ft_strlcpy(word_arr[position], s - len, len + 1);
}
position++;
}
return (0);
}
char **ft_split(char const *s, char c)
{
size_t words;
char **word_arr;
words = 0;
words = count_words(s, c);
word_arr = malloc((words + 1) * sizeof(char *));
if (!word_arr)
return (NULL);
word_arr[words] = NULL;
if (fill(word_arr, s, c))
return (NULL);
return (word_arr);
}
// int main(void)
// {
// char *s = "Hello world 123 !!! )))";
// char **ss = ft_split(s, ' ');
// while (*ss)
// {
// printf("%s\n", *ss++);
// }
// }