-
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.69 KB
/
ft_strsplit.c
File metadata and controls
71 lines (64 loc) · 1.69 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: sgigaba <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/06/01 14:59:20 by sgigaba #+# #+# */
/* Updated: 2018/08/27 07:53:25 by sgigaba ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wordlen(char const *s, char c)
{
int count;
count = 0;
while (s[count])
{
if (s[count] == c)
return (count);
count++;
}
return (count);
}
static int ft_count(char const *s, char c)
{
int count;
int in_sub;
in_sub = 0;
count = 0;
while (*s)
{
in_sub = (in_sub && *s == c) ? 0 : in_sub;
if (!in_sub && *s != c)
{
count++;
in_sub = 1;
}
s++;
}
return (count);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int elements;
char **ptr;
if (!s)
return (NULL);
elements = ft_count(s, c);
if (!(str = (char **)malloc(sizeof(char *) * (elements + 1))))
return (NULL);
ptr = str;
while (elements--)
{
while (*s == c && *s)
s++;
*str = ft_strsub(s, 0, ft_wordlen(s, c));
str++;
s = s + ft_wordlen(s, c);
}
*str = NULL;
return (ptr);
}