-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
81 lines (74 loc) · 1.77 KB
/
ft_strsplit.c
File metadata and controls
81 lines (74 loc) · 1.77 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
76
77
78
79
80
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jelefebv <jelefebv@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/25 17:24:58 by jelefebv #+# #+# */
/* Updated: 2014/12/10 01:05:14 by jelefebv ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int ft_nbr_tab(char const *s, char c)
{
int i;
int m;
i = 0;
m = 0;
while (s[i])
{
if (s[i] != c)
{
m++;
while (s[i] != c && s[i])
i++;
}
if (s[i] == c)
i++;
}
return (m);
}
static char **ft_creat_tab(char **split, char const *s, char c, int m)
{
int i;
int j;
int k;
unsigned int start;
i = 0;
j = 0;
while (s[i] && m > 0)
{
k = 0;
while (s[i] == c)
i++;
start = i;
while (s[i] != c && s[i])
{
i++;
k++;
}
split[j] = ft_strsub(s, start, k);
j++;
m--;
}
split[j] = 0;
return (split);
}
char **ft_strsplit(char const *s, char c)
{
int m;
char **split;
if (!s || c == '\0')
return (NULL);
else
{
m = ft_nbr_tab(s, c);
split = (char **)malloc((sizeof(char *) * (m + 1)));
if (split == NULL)
return (NULL);
split = ft_creat_tab(split, s, c, m);
return (split);
}
}