Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
118 lines (107 loc) · 2.48 KB
/
Copy pathft_split.c
File metadata and controls
118 lines (107 loc) · 2.48 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: omarquez <omarquez@student.42urduliz. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/28 13:31:06 by omarquez #+# #+# */
/* Updated: 2026/05/04 11:59:16 by omarquez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *str, char c)
{
int counter;
int words;
int found_word;
counter = 0;
words = 0;
found_word = 1;
while (str[counter] != '\0')
{
if (str[counter] == c)
found_word = 1;
else if (found_word == 1)
{
found_word = 0;
words ++;
}
counter ++;
}
return (words);
}
static int ft_word_len(char const *str, char c)
{
int counter;
counter = 0;
while (str[counter] != '\0')
{
if (str[counter] == c)
return (counter);
counter ++;
}
return (counter);
}
static char **ft_assign_arr(char const *s, char c)
{
char **arr;
if (s == NULL)
return (NULL);
arr = malloc((ft_count_words(s, c) + 1) * sizeof(char *));
if (arr == NULL)
return (NULL);
return (arr);
}
static void *ft_free_arr(char **arr)
{
int counter;
counter = 0;
while (arr[counter])
{
free(arr[counter]);
counter ++;
}
free(arr);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **arr;
int count;
int count_arr;
count = 0;
count_arr = 0;
arr = ft_assign_arr(s, c);
if (arr == NULL)
return (NULL);
while (s[count] != '\0')
{
if (s[count] != c)
{
arr[count_arr] = ft_substr(s, count, ft_word_len(&s[count], c));
if (arr[count_arr] == NULL)
return (ft_free_arr(arr));
count = count + ft_word_len(&s[count], c);
count_arr ++;
}
else
count ++;
}
arr[count_arr] = NULL;
return (arr);
}
/*
int main(void)
{
int counter = 0;
char *arr = "lorem ipsum dolor sit amet. Salen"
char **result = ft_split(arr, ' ');
while(counter < ft_strlen((char *) result))
{
printf("%s \n", result[counter]);
counter ++;
}
return (0);
}
*/