-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
100 lines (91 loc) · 1.98 KB
/
Copy pathft_split.c
File metadata and controls
100 lines (91 loc) · 1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yoyahya <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/19 21:25:18 by yoyahya #+# #+# */
/* Updated: 2022/10/19 21:25:20 by yoyahya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(const char *str, char c)
{
int i;
int count;
count = 0;
i = 0;
while (str[i] != '\0')
{
if (str[i] == c)
i++;
else
{
count++;
while (str[i] != c && str[i])
i++;
}
}
return (count);
}
static char *mword( const char *s, char c, int *i)
{
int len;
int j;
char *temp;
while (s[*i] == c)
(*i)++;
len = 0;
j = *i;
while (s[j] && s[j] != c)
{
len++;
j++;
}
temp = malloc((len + 1) * sizeof(char));
j = 0;
if (temp == NULL)
return (NULL);
while (s[(*i)] != '\0' && s[(*i)] != c)
{
temp[j] = s[(*i)];
j++;
(*i)++;
}
temp[j] = '\0';
return (temp);
}
static char **merror(char **tab)
{
unsigned int i;
i = 0;
while (tab[i])
free(tab[i++]);
free(tab);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **arr;
int k;
int cont;
int i;
i = 0;
k = 0;
if (s == NULL)
return (NULL);
cont = count(s, c);
arr = malloc((cont + 1) * sizeof(char *));
if (arr == NULL)
return (NULL);
while (i < cont)
{
arr[i] = mword(s, c, &k);
if (!arr[i])
return (merror(arr));
i++;
}
arr[i] = 0;
return (arr);
}