-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
99 lines (90 loc) · 1.92 KB
/
ft_split.c
File metadata and controls
99 lines (90 loc) · 1.92 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bguzel <bguzel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 19:40:15 by bguzel #+# #+# */
/* Updated: 2022/11/01 18:11:33 by bguzel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int word_counter(char const *s1, char a)
{
int k;
int i;
int l;
k = 0;
i = 0;
l = 1;
while (s1[i])
{
if (s1[i] != a && l == 1)
{
k++;
l = 0;
}
else if (s1[i] == a)
{
l = 1;
}
i++;
}
return (k);
}
int word_len(const char *src, char d)
{
int dc;
int i;
i = 0;
dc = 0;
while (src[i] == d)
i++;
while (src[i] != d && src[i])
{
i++;
dc++;
}
return (dc);
}
char *stringer(char const *src, char b)
{
char *kel;
int i;
i = 0;
kel = (char *)malloc(sizeof(char) * word_len(src, b) + 1);
while (*src && *src != b)
{
kel[i] = *(src++);
i++;
}
kel[i] = '\0';
return (kel);
}
char **ft_split(char const *s, char c)
{
size_t size;
char **result;
size_t i;
size_t j;
if (!s)
return (0);
size = word_counter(s, c);
result = malloc(sizeof(char *) * (size + 1));
if (result == NULL)
return (NULL);
i = 0;
j = 0;
while (j < size && s[i])
{
while (s[i] == c)
i++;
result[j] = stringer(&s[i], c);
while (s[i] != c && s[i])
i++;
j++;
}
result[j] = NULL;
return (result);
}