-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
77 lines (70 loc) · 1.74 KB
/
ft_split.c
File metadata and controls
77 lines (70 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jsoulet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 16:42:10 by jsoulet #+# #+# */
/* Updated: 2023/02/20 16:42:16 by jsoulet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *strdupm(char const *s, int start, int end)
{
char *dest;
int i;
i = 0;
dest = (char *)malloc(((end - start) + 1) * sizeof(char));
if (!dest)
return (NULL);
while (start < end)
{
dest[i++] = s[start++];
}
dest[i] = 0;
return (dest);
}
static int count_word(const char *s, char c)
{
int i;
int k;
k = 0;
i = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i])
{
k++;
while (s[i] != c && s[i])
i++;
}
}
return (k);
}
char **ft_split(char const *s, char c)
{
char **tab;
int start;
int i;
int j;
tab = (char **) malloc((count_word(s, c) + 1) * sizeof(char *));
if (!tab)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
start = i;
while (s[i] != c && s[i])
i++;
if (start != i)
tab[j++] = strdupm(s, start, i);
}
tab[j] = 0;
return (tab);
}