-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
103 lines (93 loc) · 2.08 KB
/
Copy pathft_split.c
File metadata and controls
103 lines (93 loc) · 2.08 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tbenz <tbenz@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/08 09:51:44 by tbenz #+# #+# */
/* Updated: 2023/09/13 09:47:10 by tbenz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_str_count(char const *s, char c)
{
size_t arr_cnt;
arr_cnt = 0;
while (*s == c)
s++;
while (*s)
{
if (*s != c)
arr_cnt++;
while (*s != c && *s)
s++;
while (*s == c && *s)
s++;
}
return (arr_cnt);
}
static void ft_free(char **arr, int i)
{
int j;
j = 0;
while (j < i)
free(arr[j++]);
free (arr);
}
static char **ft_arrgen(char const *s, char c, char **arr)
{
int i;
size_t len;
i = 0;
while (*s)
{
if (*s != c && *s)
{
len = 0;
while (*s != c && *s && ++len)
s++;
arr[i++] = ft_substr(s - len, 0, len);
if (arr[i - 1] == NULL)
{
ft_free(arr, i);
return (NULL);
}
}
if (*s)
s++;
}
arr[i] = NULL;
return (arr);
}
char **ft_split(char const *s, char c)
{
char **arr;
if (!s)
return (0);
arr = (char **)malloc((ft_str_count(s, c) + 1) * sizeof(char *));
if (!arr)
return (NULL);
arr = ft_arrgen(s, c, arr);
if (!arr)
return (NULL);
return (arr);
}
/*
#include <stdio.h>
int main(void)
{
char **tab;
int i = 0;
int j = 0;
tab = ft_split("wa sf sfd dsf" , ' ');
printf("%s", tab[3]);
if (tab[4] == NULL)
printf("234");
// while (tab[i])
// {
// printf("%s", tab[i]);
// i++;
// }
}
*/