-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
75 lines (68 loc) · 1.75 KB
/
Copy pathft_strsplit.c
File metadata and controls
75 lines (68 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tebatsai <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/02 16:36:16 by tebatsai #+# #+# */
/* Updated: 2019/05/15 20:15:11 by tebatsai ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int find_wd_count(const char *s, char d)
{
int position;
int nb;
position = 0;
nb = 0;
while (*s != '\0')
{
if (*s != d && position == 0)
{
position = 1;
nb++;
}
if (*s == d && position == 1)
position = 0;
s++;
}
return (nb);
}
static int find_wd_len(const char *s, char d)
{
int len;
len = 0;
while (*s != '\0' && *s != d)
{
len++;
s++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int wd;
int i;
i = 0;
if (!s)
return (0);
wd = find_wd_count(s, c);
str = (char**)malloc(sizeof(*str) * (find_wd_count(s, c) + 1));
if (str == NULL)
return (NULL);
while (wd > 0)
{
while (*s == c && *s != '\0')
s++;
str[i] = ft_strsub(s, 0, find_wd_len(s, c));
if (str[i] == NULL)
return (NULL);
s = s + find_wd_len(s, c);
i++;
wd--;
}
str[i] = NULL;
return (str);
}