-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
150 lines (135 loc) · 2.98 KB
/
ft_split.c
File metadata and controls
150 lines (135 loc) · 2.98 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
147
148
149
150
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <sde-silv@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/08 14:47:28 by sde-silv #+# #+# */
/* Updated: 2023/10/23 13:52:34 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
#include <string.h>
#include <stdio.h>
Count the number of words;
iterate through non-delimiter
if delimiter; count and reset the non delimiter iterator
Malloc a char pointer array for the sizeof(char *) word count + 1(for NULL);
Find the length of each word and copy to allocated memories(size of char) + 0\;
Check for the delimiter at the start and end?
Point to it using pointers from the array
Add a NULL at the end of array
*/
static int arr_length(const char *s, char c)
{
int i;
int len;
i = 0;
len = 0;
while (*s)
{
if (*s != c && i == 0)
{
i = 1;
len ++;
}
else if (*s == c)
i = 0;
s ++;
}
return (len);
}
static char *ft_copy_str(const char *s, int start, int len)
{
char *str;
int i;
i = 0;
str = malloc(sizeof(char) * (len - start + 1));
if (!str)
return (NULL);
while (start < len)
{
str[i] = s[start];
i ++;
start ++;
}
str[i] = '\0';
return (str);
}
static void *ft_free(char **arr, int loc)
{
int i;
i = 0;
while (arr[i] && i <= loc)
{
free(arr[i]);
arr[i] = NULL;
i ++;
}
free(arr);
arr = NULL;
return (NULL);
}
static char **ft_whatever(char const *s, char c, char **arr)
{
int start;
size_t i;
int str;
i = 0;
str = 0;
start = -1;
while (i <= ft_strlen(s))
{
if (s[i] != c && start < 0)
start = i;
else if ((s[i] == c || i == ft_strlen(s)) && start >= 0)
{
arr[str] = ft_copy_str(s, start, i);
if (!arr[str])
return (ft_free(arr, str));
str ++;
start = -1;
}
i++;
}
arr[str] = NULL;
return (arr);
}
char **ft_split(char const *s, char c)
{
char **arr;
arr = malloc(sizeof(char *) * (arr_length(s, c) + 1));
if (!arr)
return (NULL);
arr = ft_whatever(s, c, arr);
return (arr);
}
/*
int main(void)
{
int i ;
char const *s;
char **arr;
s = " 1 2 3 4";
arr = ft_split(s, ' ');
printf("arr%p\n", arr);
i = 0;
while (arr[i] != NULL)
{
printf("%s\n", arr[i]);
i ++;
}
i = 0;
while (arr[i] != NULL)
{
free(arr[i]);
i++;
}
free(arr);
arr = NULL;
printf("arr%p\n", arr);
return (0);
}
*/