-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
88 lines (80 loc) · 1.94 KB
/
ft_split.c
File metadata and controls
88 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_split.c :+: :+: */
/* +:+ */
/* By: cwesseli <cwesseli@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/10/17 10:21:01 by cwesseli #+# #+# */
/* Updated: 2022/10/21 10:32:53 by cwesseli ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_word(const char *s, char c)
{
int words;
int i;
words = 0;
i = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != c && s[i])
words++;
while (s[i] != c && s[i])
i++;
}
return (words);
}
static int ft_wordlen(char const *s, char c)
{
int len;
int i;
len = 0;
i = 0;
while (s[i])
{
while (s[i] != c && s[i])
{
len++;
i++;
}
return (len);
}
return (len);
}
static char **free_arr(char **arr, int rows)
{
while (rows >= 0)
{
free(arr[rows]);
rows--;
}
free(arr);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **arr;
int rows;
int cur_row;
rows = ft_count_word(s, c);
arr = malloc(sizeof(char *) * (rows + 1));
if (!arr)
return (NULL);
cur_row = 0;
while (cur_row < rows)
{
while (*s == c)
s++;
arr[cur_row] = malloc(sizeof(char) * ft_wordlen(s, c) + 1);
if (!arr[cur_row])
return (free_arr(arr, cur_row - 1));
ft_strlcpy(arr[cur_row], s, ft_wordlen(s, c) + 1);
s += ft_wordlen(s, c);
cur_row++;
}
arr[rows] = NULL;
return (arr);
}