-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
63 lines (56 loc) · 1.59 KB
/
ft_split.c
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
/* ******************************************************************************* */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By:sandraemiko<[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/25 16:13:03 by sandraemiko #+# #+# */
/* updated: 2023/02/27 21:22:10 by sandraemiko ### ########.fr */
/* */
/* ******************************************************************************* */
#include "libft.h"
static int ft_listlen(char const *s, char c)
{
int len;
len = 0;
while (*(s) != '\0')
{
if (*(s) == c)
s++;
else
{
len++;
while (*(s) != '\0' && *(s) != c)
s++;
}
}
return (len);
}
char **ft_split(char const *s, char c)
{
int i;
int j;
char **list_pointer;
if(!s)
return (NULL);
list_pointer = (char**)malloc(sizeof(char*) * (ft_listlen(s, c) + 1));
if (!list_pointer)
return (NULL);
j = 0;
while (*s != '\0')
{
i = 0;
while (*(s + i) != c && *(s + i) != '\0')
i++;
if (i != 0)
{
list_pointer[j++] = ft_substr(s, 0, i);
s += i;
}
else
s++;
}
list_pointer[j] = NULL;
return (list_pointer);
}