-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
74 lines (67 loc) · 2.07 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: vinguyen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/11 01:43:21 by vinguyen #+# #+# */
/* Updated: 2019/10/11 01:43:53 by vinguyen ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Uses malloc 3 to allocate and return a new arr of strs ending with \0
** including the arr itself. This arr is obtained by splitting s using the
** char c as a delim if alloc fails, return NULL
** Example:
** ft_strsplit("*hello*fellow***students*", ’*’)
** Returns ["hello", "fellow", "students"]
** Param: string to split, delim char
** Return: array of new strings
*/
#include "libft.h"
static int ft_strlen_firstword(char *s, char dl)
{
int count;
count = 0;
while (s && *s != dl && *s != '\0')
{
s++;
count++;
}
return (count);
}
static int ft_strsplit_helper(char const *s, char c, char **dup)
{
if (!s || !c)
return (0);
*dup = ft_strdup(s);
return (1);
}
char **ft_strsplit(char const *s, char c)
{
char *dup;
char **arr;
int i;
int j;
char *tmp;
j = 0;
if (!ft_strsplit_helper(s, c, &dup))
return (NULL);
if (!(arr = ft_memalloc(sizeof(char*) * (ft_countwords(dup, c) + 1))))
return (NULL);
while (dup && *dup)
{
if (*dup != c)
{
tmp = ft_memalloc(ft_strlen_firstword(dup, c) + 1);
i = 0;
while (*dup != c && *dup != '\0')
tmp[i++] = *dup++;
ft_strcpy(arr[j++] = ft_memalloc(ft_strlen(tmp) + 1), tmp);
free(tmp);
}
dup++;
}
return (arr);
}