-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
89 lines (80 loc) · 2.12 KB
/
ft_strsplit.c
File metadata and controls
89 lines (80 loc) · 2.12 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yyefimov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 11:45:05 by yyefimov #+# #+# */
/* Updated: 2016/12/14 15:01:43 by yyefimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count_words(char const *str, char c)
{
size_t i;
i = 0;
while (*str != '\0')
{
if (*str == c)
while (*str == c && *str)
str++;
if (*str == '\0')
break ;
while (*str && *str != c)
str++;
i++;
}
return (i);
}
static size_t ft_len(char const *str, char c)
{
size_t len;
len = 0;
while (*str != c && *str != '\0')
{
len++;
str++;
}
return (len);
}
static char **ft_makearr(char **res, const char *str, char c)
{
char **tmp;
size_t words;
size_t index;
index = 0;
words = ft_count_words(str, c);
tmp = res;
while (index < words)
{
while (*str == c && *str != '\0')
str++;
tmp[index] = ft_strsub(str, 0, ft_len(str, c));
if (tmp[index] == NULL)
{
while (index-- > 0)
free(tmp[index]);
return (NULL);
}
str = str + ft_len(str, c);
index++;
}
tmp[words] = NULL;
return (res);
}
char **ft_strsplit(char const *str, char c)
{
char **res;
size_t words;
if (!str || *str == '\0' || str == NULL)
return (ft_memalloc(sizeof(res)));
words = ft_count_words(str, c);
if (words == 0)
return (ft_memalloc(sizeof(res)));
res = (char **)malloc(sizeof(res) * words + 1);
if (res == 0)
return (ft_memalloc(sizeof(res)));
res = ft_makearr(res, str, c);
return (res);
}