-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
104 lines (94 loc) · 2.26 KB
/
ft_split.c
File metadata and controls
104 lines (94 loc) · 2.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 22:24:31 by marvin #+# #+# */
/* Updated: 2022/05/19 22:27:21 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_split_size(char const *str, char c)
{
size_t i;
size_t words;
size_t chars;
char prev;
i = 0;
prev = -1;
chars = 0;
words = 0;
while (str[i])
{
if (str[i] != c)
chars++;
if (str[i] != c && (prev == c || prev == -1))
words++;
prev = str[i];
i++;
}
if (words == 0 && chars > 0)
words = 1;
return (words);
}
static char *ft_strdup_split(char const *src, char c, size_t *i)
{
char *dst;
size_t len;
size_t j;
len = 0;
while (src[*(i) + len] && src[*(i) + len] != c)
len++;
dst = (char *)malloc(sizeof(char) * (len + 1));
if (dst == NULL)
return (NULL);
dst[len] = '\0';
j = 0;
while (j < len)
{
dst[j] = src[*(i)];
j++;
*(i) = *(i) + 1;
}
return (dst);
}
char **ft_split(char const *s, char c)
{
char **rst;
size_t deep;
size_t i;
size_t k;
i = 0;
k = 0;
if (!s)
return (NULL);
deep = ft_split_size(s, c);
rst = (char **)malloc(sizeof(char *) * (deep + 1));
if (rst == NULL)
return (NULL);
while (k < deep)
{
while (s[i] && s[i] == c)
i++;
rst[k] = ft_strdup_split(s, c, &i);
if (rst[k] == NULL)
return (NULL);
k++;
}
rst[deep] = NULL;
return (rst);
}
/*int main()
{
char a[] = "^^^1^^2a,^^^^3^^^^--h^^^^";
char **ptr;
ptr = ft_split(a, '^');
printf("%s\n", ptr[0]);
printf("%s\n", ptr[1]);
printf("%s\n", ptr[2]);
printf("%s\n", ptr[3]);
printf("%s\n", ptr[4]);
// jestem bogiem alfa
}*/