-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
73 lines (66 loc) · 1.74 KB
/
ft_strsplit.c
File metadata and controls
73 lines (66 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoufakk <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/23 11:17:42 by mmoufakk #+# #+# */
/* Updated: 2018/10/24 18:47:37 by mmoufakk ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int getnextchar(char const *s, char c)
{
int i;
i = 0;
while (s[i] != '\0' && s[i] != c)
{
i++;
}
return (i);
}
static int ft_countstring(char const *s, char c)
{
int cnt;
int ischar;
cnt = 0;
ischar = 0;
while (*s != '\0')
{
if (*s == c)
ischar = 0;
if (*s != c && ischar == 0)
{
cnt++;
ischar = 1;
}
s++;
}
return (cnt);
}
char **ft_strsplit(char const *s, char c)
{
char **foo;
int index;
if (!s)
return (NULL);
index = 0;
foo = (char **)malloc(sizeof(char *) * (ft_countstring(s, c) + 1));
if (!foo)
return (NULL);
while (*s != '\0' && ft_countstring(s, c) > 0)
{
while (*s == c && *s != '\0')
s++;
while (*s != '\0' && *s != c)
{
if (!(foo[index] = ft_strsub(s, 0, getnextchar(s, c))))
return (NULL);
s = s + (getnextchar(s, c));
index++;
}
}
foo[index] = 0;
return (foo);
}