-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
101 lines (93 loc) · 2.1 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aerrajiy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/09 12:19:17 by aerrajiy #+# #+# */
/* Updated: 2022/10/18 22:37:37 by aerrajiy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int words_count(char *s, char c)
{
int nb_strings;
int i;
i = 0;
nb_strings = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
while (s[i] != '\0' && s[i] != c)
i++;
nb_strings++;
}
if (s[i])
i++;
}
return (nb_strings);
}
static char *insert_cpy(char **ptr, char *str, char c)
{
int i;
int k;
int len;
int lop;
i = 0;
k = 0;
len = 0;
lop = 0;
while (str[len] == c)
len++;
while ((str + len)[k] != c && (str + len)[k] != '\0')
k++;
*ptr = malloc((k + 1) * sizeof(char));
if (*ptr != NULL)
{
lop = len;
while (str[lop] != '\0' && str[lop] != c)
{
(*ptr)[i++] = str[lop++];
}
(*ptr)[i] = '\0';
return (str + (k + len));
}
return (NULL);
}
static void free_me(char **tab, int i)
{
while (i >= 0)
{
free(tab[i--]);
}
free(tab);
}
char **ft_split(char const *s, char c)
{
char **tabs;
int i;
int count;
char *ppp;
ppp = (char *)s;
if (!ppp)
return (NULL);
i = -1;
count = words_count(ppp, c);
tabs = (char **)malloc(sizeof(char *) * (count + 1));
if (tabs != NULL)
{
tabs[count] = NULL;
while (++i < count)
{
ppp = insert_cpy(tabs + i, ppp, c);
if (ppp == NULL)
{
free_me(tabs, i);
return (NULL);
}
}
}
return (tabs);
}