-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
101 lines (92 loc) · 2 KB
/
Copy pathft_split.c
File metadata and controls
101 lines (92 loc) · 2 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
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alexfran <alexfran@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/26 17:05:29 by alexfran #+# #+# */
/* Updated: 2025/12/02 12:58:27 by alexfran ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_c(const char *s, char c)
{
int i;
int count;
int flag;
count = ((i = 0));
flag = 0;
while (s[i])
{
if (s[i] != c && flag == 0)
{
count++;
flag = 1;
}
if (s[i] == c)
flag = 0;
i++;
}
return (count);
}
static void *ft_free(char **res, int k)
{
int i;
i = 0;
if (!res)
return (NULL);
while (i < k)
{
free(res[i]);
i++;
}
free(res);
return (NULL);
}
static char *alloc_word(const char *s, int start, char c)
{
char *word;
int i;
int len;
len = 0;
while (s[start + len] != c && s[start + len])
len++;
word = malloc(sizeof(char) * (len + 1));
if (!word)
return (NULL);
i = 0;
while (i < len)
{
word[i] = s[start + i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
char **res;
int i;
int j;
i = 0;
j = 0;
if (!s)
return (NULL);
res = malloc (sizeof(char *) * (count_c(s, c) + 1));
if (!res)
return (NULL);
while (s[i])
{
if (s[i] != c && (i == 0 || s[i - 1] == c))
{
res[j] = alloc_word(s, i, c);
if (!res[j])
return (ft_free(res, j));
j++;
}
i++;
}
res[j] = NULL;
return (res);
}