-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
108 lines (97 loc) · 2.09 KB
/
ft_split.c
File metadata and controls
108 lines (97 loc) · 2.09 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
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sben-ela <sben-ela@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 10:05:16 by sben-ela #+# #+# */
/* Updated: 2022/10/28 13:20:10 by sben-ela ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static int ft_lenword(char const *str, char c)
{
int i;
i = 0;
while (str[i] && str[i] != c)
i++;
return (i);
}
static char *ft_fullstr(char const *str, char c)
{
char *word;
int i;
int len;
i = 0;
len = ft_lenword(str, c);
word = malloc(len + 1);
if (!word)
return (NULL);
while (i < len)
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
static void ft_free(char **strs, int i)
{
int j;
j = 0;
while (j < i)
{
free(strs[j]);
j++;
}
free(strs);
}
static char **ft_second(char **strs, const char *str, int c)
{
int i;
i = 0;
while (*str)
{
while (*str && *str == c)
str++;
if (*str)
{
strs [i] = ft_fullstr(str, c);
if (!strs [i])
{
ft_free(strs, i);
return (0);
}
i++;
}
while (*str && *str != c)
str++;
}
strs [i] = 0;
return (strs);
}
char **ft_split(char const *str, char c)
{
char **strs;
int count;
int i;
count = 1;
i = 0;
if (!str)
return (NULL);
while (str [i])
{
while (str [i] && str [i] == c)
i++;
if (str [i])
count ++;
while (str[i] && str [i] != c)
i++;
}
strs = (char **)malloc(sizeof(char *) * count);
if (!strs)
return (NULL);
strs = ft_second(strs, str, c);
return (strs);
}