-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
executable file
·89 lines (80 loc) · 1.94 KB
/
ft_split.c
File metadata and controls
executable file
·89 lines (80 loc) · 1.94 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: allopez <allopez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/30 13:24:31 by allopez #+# #+# */
/* Updated: 2021/08/30 13:24:33 by allopez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int word_count(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
count++;
i++;
}
return (count);
}
int word_length(char const *s, char c)
{
int i;
int len;
i = 0;
len = 0;
while (s[i] != c && s[i] != '\0')
{
i++;
len++;
}
return (len);
}
char **f(char const *s, char c, char **result, int words_count)
{
int i;
int j;
int w_len;
while (*s == c)
s++;
i = -1;
while (++i < words_count)
{
while (*s == c)
s++;
j = 0;
w_len = word_length(s, c);
result[i] = (char *)malloc(sizeof(char) * (w_len + 1));
if (!(result[i]))
return (NULL);
while (j < w_len)
{
result[i][j] = *s;
s++;
j++;
}
result[i][j] = '\0';
}
return (result);
}
char **ft_split(char const *s, char c)
{
char **result;
int wcount;
if (!s)
return (NULL);
wcount = word_count(s, c);
result = (char **)malloc(sizeof(char *) * (wcount + 1));
if (!(result))
return (NULL);
result = f(s, c, result, wcount);
result[wcount] = NULL;
return (result);
}