-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
73 lines (66 loc) · 1.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zbabahmi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/31 05:10:50 by zbabahmi #+# #+# */
/* Updated: 2022/10/31 05:10:51 by zbabahmi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int counter(const char *tpm, char c)
{
int i;
int count;
i = 0;
count = 0;
while (*tpm)
{
if (*tpm != c && count == 0)
{
count = 1;
i++;
}
else if (*tpm == c)
count = 0;
tpm++;
}
return (i);
}
int size_word(char const *s, char c, int i)
{
int size;
size = 0;
while (s[i] != c && s[i])
{
size++;
i++;
}
return (size);
}
char **ft_split(char const *s, char c)
{
int i;
int word;
int size;
char **sp;
int j;
i = 0;
j = -1;
word = counter(s, c);
sp = (char **)malloc((word + 1) * sizeof(char *));
if (!sp)
return (0);
while (++j < word)
{
while (s[i] == c)
i++;
size = size_word(s, c, i);
sp[j] = ft_substr(s, i, size);
i += size;
}
sp[j] = 0;
return (sp);
}