Repository navigation
Expand file tree
/
Copy pathft_strsplit.c
More file actions
94 lines (85 loc) · 2.11 KB
/
Copy pathft_strsplit.c
File metadata and controls
94 lines (85 loc) · 2.11 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
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eblackbu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/06 11:56:57 by eblackbu #+# #+# */
/* Updated: 2019/09/07 14:11:46 by eblackbu ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static unsigned int count_words(const char *str, char separator)
{
size_t i;
unsigned int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] != separator)
{
count++;
while (str[i] != separator && str[i])
i++;
}
else
i++;
}
return (count);
}
static char *ft_new_str(const char *str, char separator)
{
size_t i;
size_t len;
char *newstr;
i = 0;
len = 0;
while (str[len] != separator && str[len])
len++;
if (!(newstr = (char*)malloc(sizeof(char) * (len + 1))))
return (NULL);
while (i < len)
{
newstr[i] = str[i];
i++;
}
newstr[i] = '\0';
return (newstr);
}
static char **free_list(char **list)
{
unsigned int i;
i = 0;
while (list[i])
free(list[i++]);
free(list);
return (NULL);
}
char **ft_strsplit(char const *s, char c)
{
unsigned int i;
unsigned int j;
unsigned int words;
char **list;
i = 0;
j = 0;
if (!s)
return (NULL);
words = count_words(s, c);
if (!(list = (char**)malloc(sizeof(char*) * (words + 1))))
return (NULL);
while (i < words)
{
while (s[j] == c)
j++;
if (!(list[i] = ft_new_str(&s[j], c)))
return (free_list(list));
j += (unsigned int)ft_strlen(list[i]);
i++;
}
list[i] = NULL;
return (list);
}