-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
71 lines (64 loc) · 1.68 KB
/
ft_strsplit.c
File metadata and controls
71 lines (64 loc) · 1.68 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gfoote <gfoote@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 11:22:00 by gfoote #+# #+# */
/* Updated: 2019/04/10 16:37:10 by gfoote ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_words_count(char const *s, char c)
{
int result;
int i;
result = 0;
i = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
result++;
while (s[i] && (s[i] != c))
i++;
}
return (result);
}
static char **ft_result(char const *s, char **result, char c)
{
int i;
int j;
int k;
i = 0;
j = 0;
k = 0;
while (s[i])
{
while (s[i] == c)
i++;
j = i;
while (s[i] && s[i] != c)
i++;
if (i > j)
{
result[k++] = ft_strndup(s + j, i - j);
if (!result)
ft_strdel(result);
}
}
result[k] = 0;
return (result);
}
char **ft_strsplit(char const *s, char c)
{
char **result;
if (!s || !c)
return (NULL);
result = (char **)malloc(sizeof(char *) * (ft_words_count(s, c) + 1));
if (!result)
return (NULL);
return (ft_result(s, result, c));
}