-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
66 lines (61 loc) · 1.77 KB
/
Copy pathft_strsplit.c
File metadata and controls
66 lines (61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maloua-h <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/03 17:49:56 by maloua-h #+# #+# */
/* Updated: 2019/02/04 17:13:38 by maloua-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *word_builder(char const *str, char c, unsigned int *index)
{
unsigned int i;
unsigned int start;
char *word;
while (str[*index] == c && str[*index] != '\0')
(*index)++;
start = *index;
while (str[*index] != c && str[*index] != '\0')
(*index)++;
word = ft_strnew(*index - start);
if (!word)
return (NULL);
i = 0;
while (start < *index)
{
word[i] = str[start];
start++;
i++;
}
word[i] = '\0';
return (word);
}
char **ft_strsplit(char const *s, char c)
{
unsigned int i;
unsigned int j;
unsigned int words;
char **rtn;
if (!s)
return (NULL);
i = 0;
words = 0;
while (s[i] != '\0')
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
words++;
i++;
}
i = 0;
j = 0;
rtn = (char**)malloc(sizeof(char*) * (words + 1));
if (!rtn)
return (NULL);
while (i < words)
rtn[i++] = word_builder(s, c, &j);
rtn[i] = 0;
return (rtn);
}