-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
120 lines (108 loc) · 2.49 KB
/
Copy pathft_split.c
File metadata and controls
120 lines (108 loc) · 2.49 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
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: etchipoq <etchipoq@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/19 19:19:16 by etchipoq #+# #+# */
/* Updated: 2025/11/05 11:28:45 by etchipoq ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(char const *str, char sep)
{
int i;
int word;
i = 0;
word = 0;
while (str[i])
{
while (str[i] != '\0' && str[i] == sep)
i++;
if (str[i] != '\0')
word++;
while (str[i] != '\0' && str[i] != sep)
i++;
}
return (word);
}
static char *new_word(int start, int finish, char const *str)
{
int i;
char *word;
if (finish <= start)
return (NULL);
word = malloc(sizeof(char) * (finish - start + 1));
if (!word)
return (NULL);
i = 0;
while (start + i < finish)
{
word[i] = str[i + start];
i++;
}
word[i] = '\0';
return (word);
}
static char **clearar(char **array, int size)
{
int i;
i = 0;
while (i < size)
free(array[i++]);
free(array);
return (NULL);
}
static char **plit(char **array, const char *str, char c)
{
int i;
int word;
int start;
int end;
i = 0;
word = 0;
while (str[i] != '\0')
{
while (str[i] && str[i] == c)
i++;
start = i;
while (str[i] && str[i] != c)
i++;
end = i;
if (end > start)
{
array[word] = new_word(start, end, str);
if (array[word] == NULL)
return (clearar(array, word));
word++;
}
}
array[word] = NULL;
return (array);
}
char **ft_split(char const *str, char c)
{
char **array;
if (!str)
return (NULL);
array = malloc(sizeof(char *) * (count(str, c) + 1));
if (!array)
return (NULL);
return (plit(array, str, c));
}
/* #include <stdio.h>
int main(void)
{
char **res= ft_split(" tripouille 42 ", 0);
if (res == NULL || !res[0])
{
printf("rip\n");
return (0);
}
printf("primeiro: %s \n segundo: %s\n ", res[0], res[1]);
free(res[0]);
free(res[1]);
free(res[2]);
free(res);
} */