-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split_old.c
More file actions
146 lines (133 loc) · 3.83 KB
/
ft_split_old.c
File metadata and controls
146 lines (133 loc) · 3.83 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_old.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acastrov <acastrov@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/25 20:17:02 by acastrov #+# #+# */
/* Updated: 2024/09/27 18:21:56 by acastrov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// Static, counts the number of substrings of s separated by char c
static size_t ft_substrcount(char const *s, char c)
{
size_t string_count;
string_count = 0;
while (*s)
{
while (*s == c)
s++;
if (*s)
string_count++;
while (*s && *s != c)
s++;
}
return (string_count);
}
// Splits one string into different substring by an specific char
char **ft_split(char const *s, char c)
{
char **split;
char **splitf;
int i;
char *fc;
size_t sc;
if (!s || !c)
return (NULL);
sc = ft_substrcount(s, c) + 1;
split = malloc(sc * sizeof(char *));
if (!split)
return (NULL);
splitf = split;
while (*s)
{
while (*s == c)
s++;
if (*s != c && *s)
{
fc = ft_strchr((char *)s, c);
if (!fc)
fc = (char *)s + ft_strlen(s);
*split = ft_substr(s, 0, fc - (char *)s);
if (!*split)
{
while (!--sc) // No acompana pos split
{
free(*split);
split--;
}
return (free(split), NULL);
}
split++;
sc--;
s = fc; // Bucle infinito aqui
}
}
*split = NULL;
return (splitf);
}
#include <stdio.h>
#include <stdlib.h>
#include "libft.h"
int main(void)
{
char **result;
int i;
// Test 1
char *test1 = "Hello World from 42!";
printf("Test 1: Splitting \"%s\" by space\n", test1);
result = ft_split(test1, ' ');
i = 0;
while (result[i])
{
printf("result[%d]: %s\n", i, result[i]);
free(result[i]);
i++;
}
free(result);
// Test 2
char *test2 = ",,,,Hello,,World,,";
printf("Test 2: Splitting \"%s\" by comma\n", test2);
result = ft_split(test2, ',');
i = 0;
while (result[i])
{
printf("result[%d]: %s\n", i, result[i]);
free(result[i]);
i++;
}
free(result);
// Test 3
char *test3 = "NoDelimiterHere";
printf("Test 3: Splitting \"%s\" by space\n", test3);
result = ft_split(test3, ' ');
i = 0;
while (result[i])
{
printf("result[%d]: %s\n", i, result[i]);
free(result[i]);
i++;
}
free(result);
// Test 4
char *test4 = "";
printf("Test 4: Splitting \"%s\" by any delimiter\n", test4);
result = ft_split(test4, ' ');
if (result[0] == NULL)
printf("result: empty\n");
free(result);
return 0;
}
/* Primer creamos un array de char
Para determinar ese array de char, primero debemos encontrar n numero de coincidencias de c
Una vez que hemos creado nuestro array de char, tenemos que rellenarlo con un trim de cada una de las coincidencias
No nos vale nuestro trim normal? Apunta hasta el final del total de la string
Si simplemente pudieramos hacerlo hasta n caracteres o hasta la siguiente coincidencia
strchr + (strch + 1) para limitar mejor ese trim?
Y repetimos el proceso en la direccion que devuelva strch + 1 etc.*/
/* Otra aproximacion
Sacamos primero la cuenta de palabras que estan separadas por el limitador para hacer el array
Despues vamos rellenando esos huecos con strdup*
Free si falla*/