-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
87 lines (80 loc) · 1.95 KB
/
Copy pathft_split.c
File metadata and controls
87 lines (80 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asuc <asuc@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/25 18:51:56 by asuc #+# #+# */
/* Updated: 2023/11/09 02:26:18 by asuc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strdup_split(const char *src, char charset)
{
char *tab;
int i;
i = 0;
while (src[i] != charset && src[i])
i++;
tab = malloc(sizeof(char) * (i + 1));
if (tab == NULL || src == NULL)
return (tab);
i = 0;
while (src[i] && src[i] != charset)
{
tab[i] = src[i];
i++;
}
tab[i] = '\0';
return (tab);
}
static int count_word(const char *str, char charset)
{
int i;
int count;
int bol;
count = 0;
i = 0;
bol = 1;
while (str[i])
{
if (str[i] != charset)
{
if (bol == 1)
count++;
bol = 0;
}
else
bol = 1;
i++;
}
return (count);
}
char **ft_split(const char *str, char charset)
{
char **ret;
int i;
int j;
if (!str)
return (NULL);
ret = NULL;
j = 0;
i = 0;
if (!str)
return (NULL);
ret = malloc(sizeof(char *) * (count_word(str, charset) + 1));
if (ret == NULL)
return (ret);
while (i < count_word(str, charset))
{
while (str[j] == charset && str[j] != 0)
j++;
ret[i] = ft_strdup_split(str + j, charset);
i++;
while (str[j] != charset && str[j] != 0)
j++;
}
ret[i] = 0;
return (ret);
}