-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
86 lines (81 loc) · 1.83 KB
/
ft_split.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
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thuy-ngu <thuy-ngu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/08 12:59:17 by thuy-ngu #+# #+# */
/* Updated: 2023/10/22 15:47:41 by thuy-ngu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_countword(char const *s, char c)
{
size_t count;
if (!*s)
return (0);
count = 0;
while (*s)
{
while (*s == c)
{
s++;
}
if (*s)
count++;
while (*s != c && *s)
{
s++;
}
}
return (count);
}
char **ft_split(char const *s, char c)
{
char **lst;
int i;
int j;
size_t start;
lst = (char **)malloc(sizeof(char *) * ((ft_countword(s, c)) + 1));
if (!lst)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
start = i;
if (s[i])
{
while (s[i] != c && s[i])
i++;
lst[j++] = ft_substr(s, start, (i - start));
}
}
lst[j] = NULL;
return (lst);
}
/*int main(void)
{
const char *s = "it,is,something";
char **result = ft_split(s, ',');
if (result)
{
int i = 0;
while (result[i] != NULL)
{
int j = 0;
while (result[i][j] != '\0')
{
printf("%c", result[i][j]);
j++;
}
printf("\n");
free(result[i]);
i++;
}
free(result);
}
}*/