-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
77 lines (70 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hsaadi <hsaadi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/25 11:21:50 by hsaadi #+# #+# */
/* Updated: 2022/10/29 03:04:19 by hsaadi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/libft.h"
static int w_count(char const *str, char c)
{
int i;
int flag;
int w_count;
i = 0;
flag = 0;
w_count = 0;
while (str[i])
{
if (str[i] == c && flag == 1)
flag = 0;
else if ((str[i] != c) && flag == 0)
{
flag = 1;
w_count++;
}
i++;
}
return (w_count);
}
static char *spliter(const char *s, char c, int *start)
{
char *str;
int i;
int j;
j = *start;
i = 0;
while (s[j] == c)
j++;
while (s[j] && s[j] != c)
{
j++;
i++;
}
str = ft_substr(s, j - i, i);
*start = j;
return (str);
}
char **ft_split(char const *s, char c)
{
char **tab;
int i;
int j;
if (!s)
return (NULL);
i = 0;
tab = ft_calloc(sizeof(char *), w_count(s, c) + 1);
if (!tab)
return (NULL);
j = 0;
while (i < w_count(s, c))
{
tab[i] = spliter(s, c, &j);
i++;
}
return (tab);
}