-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
109 lines (99 loc) · 2.15 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
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adashyan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 22:40:04 by adashyan #+# #+# */
/* Updated: 2022/05/08 18:33:31 by adashyan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wordcount(char const *s, char c)
{
int i;
int flag;
int k;
i = 0;
flag = 1;
k = 0;
while (s[i])
{
if (s[i] == c)
flag = 1;
else if (flag)
{
k++;
if (s[i] != '\0' && s[i] != c)
flag = 0;
}
i++;
}
return (k);
}
static int ft_wordlen(char const *s, char c, int i)
{
int len;
len = 0;
while (s[i] && s[i] != c)
{
i++;
len++;
}
return (len);
}
static char *ft_strdup2(const char *s, char c, int len, int *i)
{
char *ptr;
int j;
j = 0;
ptr = (char *)malloc(sizeof(char) * (len + 1));
if (!ptr)
return (NULL);
while (s[*i] && s[*i] != c)
{
ptr[j] = s[*i];
j++;
*i += 1;
}
ptr[j] = '\0';
return (ptr);
}
static void *ft_free(char **splitted, int j)
{
while (splitted[j])
{
free(splitted[j]);
j++;
}
free(splitted);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **splitted;
int i;
int j;
i = 0;
j = 0;
if (!s)
return (NULL);
splitted = (char **)malloc(sizeof(char *) * (ft_wordcount(s, c) + 1));
if (!splitted)
return (NULL);
while (s[i])
{
if (s[i] != c)
{
splitted[j] = ft_strdup2(s, c, ft_wordlen(s, c, i) + 1, &i);
if (!splitted[j++])
return (ft_free(splitted, j - 1));
}
if (!s[i])
break ;
i++;
}
splitted[j] = NULL;
return (splitted);
}