-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
65 lines (60 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/04 17:00:22 by lmartine #+# #+# */
/* Updated: 2018/03/10 20:30:57 by lmartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_words(char const *s, char c, int *index)
{
int i;
int start;
char *word;
i = 0;
while (s[*index] == c && s[*index])
(*index)++;
start = *index;
while (s[*index] != c && s[*index])
(*index)++;
if (!(word = ft_strnew(*index - start)))
return (NULL);
while (start < *index)
{
word[i] = s[start];
i++;
start++;
}
word[i] = '\0';
return (word);
}
char **ft_strsplit(char const *s, char c)
{
char **temp;
int i;
int j;
int count;
i = 0;
j = 0;
count = 0;
if (!s || !c)
return (NULL);
while (s[i] != '\0')
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
count++;
i++;
}
i = 0;
temp = (char **)malloc((count + 1) * sizeof(char *));
if (!temp)
return (NULL);
while (i < count)
temp[i++] = ft_words(s, c, &j);
temp[i] = 0;
return (temp);
}