-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
87 lines (78 loc) · 1.94 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
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <azaghlou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/18 18:51:00 by azaghlou #+# #+# */
/* Updated: 2022/10/25 02:57:49 by azaghlou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_cnt(char const *str, char x)
{
int i;
int count;
count = 0;
i = 0;
while (str[i])
{
if (str[i] != x)
{
count++;
while (str[i] && str[i] != x)
i++;
}
else
i++;
}
return (count);
}
static int ft_len(char const *ss, char cc, int i)
{
int j;
j = 0;
while (ss[i] && ss[i] != cc)
{
j++;
i++;
}
return (j);
}
char **ft_split(char const *s, char c)
{
char **aptx;
int i;
int j;
i = 0;
j = 0;
if (!s)
return (0);
aptx = (char **)malloc(sizeof(char *) * (ft_cnt(s, c) + 1));
if (!aptx)
return (NULL);
while (i < ft_cnt(s, c))
{
while (s[j] && s[j] == c)
j++;
aptx[i] = ft_substr(s, j, ft_len(s, c, j));
i++;
j += ft_len(s, c, j);
}
aptx[i] = 0;
return (aptx);
}
// int main()
// {
// char *s = "ftgrtgg bg tt djdj schlussfolgerung t .";
// printf("%d",ft_cnt(s, ' '));
// char **array = ft_split(s, ' ');
// printf("%s \n", array[0]);
// printf("%s \n", array[1]);
// printf("%s \n", array[2]);
// }
// int main()
// {
// printf(":%d:\n", ft_cnt("amine rojola ", ' '));
// }