-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
74 lines (67 loc) · 1.7 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atilegen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/22 12:34:58 by atilegen #+# #+# */
/* Updated: 2018/03/25 17:40:25 by atilegen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int splits(const char *str, char c)
{
int count;
int i;
i = 1;
if (*str == '\0')
return (0);
count = 0;
if (str[0] != c)
count++;
while (str[i])
{
if (str[i] != c && str[i - 1] == c)
count++;
i++;
}
return (count);
}
static int len(const char *str, int j, int c)
{
int k;
k = 0;
while (str[j] != c && str[j])
{
j++;
k++;
}
return (k);
}
char **ft_strsplit(char const *s, char c)
{
char **arr;
int w;
int j;
int i;
i = 0;
j = 0;
if (!s)
return (0);
arr = (char**)malloc(sizeof(char*) * (splits(s, c) + 1));
if (!arr)
return (0);
while (i < splits(s, c))
{
while (s[j] == c && s[j])
j++;
w = 0;
arr[i] = malloc(sizeof(char) * (len(s, j, c) + 1));
while (s[j] != c && s[j])
arr[i][w++] = s[j++];
arr[i++][w] = '\0';
}
arr[i] = 0;
return (arr);
}