-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
83 lines (75 loc) · 1.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: youel-id <youel-id@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/15 23:34:02 by youel-id #+# #+# */
/* Updated: 2022/10/29 18:34:09 by youel-id ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int f_count(const char *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != c && s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static size_t f_len(const char *s, char c)
{
size_t i;
i = 0;
while (s[i] && s[i] != c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
char **str;
int i;
int k;
i = 0;
k = 0;
while (!s)
return (NULL);
str = (char **)malloc(sizeof(char *) * (f_count(s, c) + 1));
if (!str)
return (NULL);
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != '\0')
{
str[k] = ft_substr(s + i, 0, f_len(s + i, c));
k++;
while (s[i] && s[i] != c)
i++;
}
}
str[k] = NULL;
return (str);
}
/*{
int i;
i = 0;
char *str = "lorem ipsum dolor sit amet, consectetur ad
char **str2 = ft_split(str,'i');
while(str2[i])
{
printf("%s\n",str2[i]);
i++;
}
}*/