-
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.76 KB
/
Copy pathft_strsplit.c
File metadata and controls
75 lines (68 loc) · 1.76 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: eleclet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/01 18:58:18 by eleclet #+# #+# */
/* Updated: 2015/12/08 11:27:21 by eleclet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strhelp(char const *s, unsigned int start, size_t len)
{
char *str;
str = (char *)malloc(sizeof(*str) * len + 1);
while (start < len)
{
*str = s[start];
start++;
str = str + 1;
}
*str = '\0';
return (str - len);
}
static int ft_nb_mots(char const *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
while (s[i] && s[i] != c)
i++;
if (s[i] != '\0' || s[i - 1] != c)
j++;
}
return (j);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int i;
int j;
int l;
i = 0;
j = 0;
if (s == NULL)
return (NULL);
l = ft_nb_mots(s, c);
str = (char **)malloc(sizeof(char *) * l + 1);
while (i < l)
{
j = 0;
while (*s && *s == c)
s = s + 1;
while (*(s + j) && *(s + j) != c)
j++;
*(str++) = ft_strhelp(s, 0, j);
s = s + j;
i++;
}
*str = NULL;
return (str - l);
}