-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
89 lines (80 loc) · 1.93 KB
/
ft_split.c
File metadata and controls
89 lines (80 loc) · 1.93 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edforte <edforte@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 14:20:20 by edforte #+# #+# */
/* Updated: 2024/01/24 20:55:13 by edforte ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_mkword(char const *str, int start, int end)
{
int i;
char *substr;
i = 0;
substr = (char *)malloc(((end - start) + 2) * sizeof(char));
while (start <= end)
{
substr[i] = str[start];
i ++;
start ++;
}
substr[i] = '\0';
return (substr);
}
int wds_count(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i ++;
if (s[i])
count ++;
while (s[i] && s[i] != c)
i ++;
}
return (count);
}
char **memall(char const *s, char c)
{
char **words;
if (!s)
return (NULL);
words = (char **)malloc((1 + wds_count(s, c)) * sizeof(char *));
if (!words)
return (NULL);
return (words);
}
char **ft_split(char const *s, char c)
{
int i;
int start;
char **words;
int count;
words = memall(s, c);
if (!words)
return (NULL);
i = 0;
count = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i ++;
if (s[i])
{
start = i;
while (s[i] && s[i] != c)
i ++;
words[count++] = ft_mkword(s, start, (i - 1));
}
}
words[count] = NULL;
return (words);
}