-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
70 lines (65 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdemir <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/09 20:41:10 by akdemir #+# #+# */
/* Updated: 2023/07/15 18:46:57 by akdemir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t count(const char *p, char c)
{
size_t i;
size_t len;
i = 0;
len = 0;
while (p[i])
{
while (p[i] == c && p[i])
i++;
if (p[i] != c && p[i])
{
len++;
while (p[i] != c && p[i])
i++;
}
}
return (len);
}
char **ft_split(char const *s, char c)
{
char **list;
size_t i;
size_t tmp;
size_t l;
i = 0;
l = 0;
list = (char **)malloc(sizeof(char *) * (count(s, c) + 1));
if (!list)
return (NULL);
while (s[i])
{
while (s[i] && s[i] == c)
i++;
tmp = i;
while (s[tmp] && s[tmp] != c)
tmp++;
printf("ilk");
if (count(s, c) == l)
break ;
list[l++] = ft_substr(s, i, tmp - i);
i = tmp;
printf("son");
}
list[l] = 0;
return (list);
}
int main()
{
char a[]="ak,if";
char **b=ft_split(a,',');
printf("%s%s%s",*b,b[1],b[2]);
}