-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
executable file
·76 lines (69 loc) · 1.64 KB
/
ft_strsplit.c
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
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: achambon <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 14:55:39 by achambon #+# #+# */
/* Updated: 2017/01/06 12:08:35 by achambon ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int ft_words(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
if (s[i] != c)
{
j++;
while (s[i] != c && s[i])
i++;
}
else
i++;
}
return (j);
}
static int ft_letter(const char *s, char c, int i)
{
int len;
len = 0;
while (s[i] != c && s[i])
{
i++;
len++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int j;
int len;
char **tab;
i = 0;
j = 0;
if (!s || !(tab = (char**)malloc(sizeof(*tab) * ft_words(s, c) + 1)))
return (NULL);
while (s[i])
{
if (s[i] != c)
{
len = ft_letter(s, c, i);
if (!(tab[j] = ft_strsub(s, i, len)))
return (NULL);
j++;
i = i + len;
}
else
i++;
}
tab[j] = NULL;
return (tab);
}