Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
125 lines (115 loc) · 2.45 KB
/
Copy pathft_split.c
File metadata and controls
125 lines (115 loc) · 2.45 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
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chguerre <chguerre@student.lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 18:00:47 by chguerre #+# #+# */
/* Updated: 2025/12/09 10:04:46 by chguerre ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <stdlib.h>
void freememory(char **str, size_t count)
{
size_t i;
i = 0;
while (i < count)
{
free(str[i]);
i++;
}
free(str);
}
char *get_new_word(const char **s, char c)
{
size_t len;
char *start;
char *word;
while (**s == c)
(*s)++;
start = (char *)*s;
len = 0;
while (**s && **s != c)
{
len++;
(*s)++;
}
word = ft_substr(start, 0, len);
if (!word)
return (0);
return (word);
}
size_t countwords(char const *s, char c)
{
size_t count;
int insideword;
count = 0;
while (*s)
{
insideword = 0;
while (*s == c && *s)
s++;
while (*s != c && *s)
{
if (!insideword)
{
insideword = 1;
count++;
}
s++;
}
}
return (count);
}
char **ft_split(char const *s, char c)
{
size_t qywords;
char **arraystring;
size_t i;
char *temp;
qywords = countwords(s, c);
arraystring = (char **)malloc((qywords + 1) * sizeof(char *));
if (!arraystring)
return (NULL);
i = 0;
while (i < qywords)
{
temp = get_new_word((const char **)&s, c);
if (temp != NULL)
arraystring[i] = temp;
else
{
freememory(arraystring, i);
return (NULL);
}
i++;
}
arraystring[qywords] = NULL;
return (arraystring);
}
/*
int main(void)
{
char *str;
char separador;
char **resultado;
int i;
str = "wdqqwdqw";
separador = ' ';
printf("[%c]\n", separador);
printf("La cadena completa es %s\n", str);
// countwords(str, separador);
resultado = ft_split(str, separador);
i = 0;
while (resultado[i] != NULL)
{
printf("-%s\n", resultado[i]);
i++;
}
freememory(resultado);
return (0);
}
*/