-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
145 lines (132 loc) · 4.29 KB
/
ft_split.c
File metadata and controls
145 lines (132 loc) · 4.29 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: antandre <antandre@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 12:17:47 by antandre #+# #+# */
/* Updated: 2024/06/04 16:53:37 by antandre ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
/*
* 1. 'while' will execute until the end of the string 's' is reached.
* 2. Inside the loop, it checks if the current character 's[i]'
* is different from the delimiter character 'c'.
* 3. If 's[i]' is not equal to 'c', it means a word has been found.
* Increment 'count' by 1 and then move 'i' to the next character
* that is not 'c' or to the end of the string.
* 4. If 's[i]' is equal to 'c', it means delimiter character has been found.
* Move 'i' to the next character.
* 5. Once the entire string 's' has been traversed, return
* the value of 'count', which represents the total number of words found.
*/
static int count_words(char const *s, char c)
{
int count;
int i;
count = 0;
i = 0;
while (s[i])
{
if (s[i] != c)
{
count++;
while (s[i] && s[i] != c)
i++;
}
else
i++;
}
return (count);
}
/*
* 1. Initialize a variable i with the value of *start. This is done to
* ensure that the search for the next word starts from
* the correct position in the string.
* 2. 'while' loop to skip any delimiter 'c' at the beginning of the word.
* 3. After the loop, 'i' will point to the first character of the next
* word in the string. Save its value in 'start'.
* 4. Another 'while' loop to find the end of the word ('\0' or 'c').
* 5. Use ft_substr to extract the word from the string 's'.
* 6. Update the value of '*start' with the value of 'i', so that the next
* call to get_next_word starts from the correct position in the string.
*/
static char *get_next_word(char const *s, char c, int *start)
{
int i;
char *word;
i = *start;
while (s[i] && s[i] == c)
i++;
*start = i;
while (s[i] && s[i] != c)
i++;
word = ft_substr(s, *start, i - *start);
*start = i;
return (word);
}
/*
* Function to free the memory allocated for the words
* in the 'result' array in case an error occurs while getting
* a word.
*/
static void free_result(char **result)
{
int i;
i = 0;
while (result[i])
{
free(result[i]);
i++;
}
free(result);
}
/*
* Split a string into words separated by a delimiter character.
* 1. Check if the string 's' is NULL. If so,
* the function returns NULL to indicate an error.
* 2. Use the 'count_words' function to count the number of words
* in the string 's'.
* 3. Allocate memory for the 'result' array using the malloc function.
* 4. 'while' loop that executes word_count times. In each iteration:
* - Use the 'get_next_word' function to retrieve the next word from
* the string 's'.
* - If it returns NULL, it indicates an error occurred while
* fetching the word.
* In this case, free the memory allocated for previous words
* and for the 'result'array, and return NULL to indicate an error.
* - If it returns a valid word, assign that word to the result[i]
* element of the array.
* - Increment the counter 'i' to move to the next position in the array.
* 5. Assign NULL to the last element of the 'result' array to
* mark the end of the array.
*/
char **ft_split(char const *s, char c)
{
char **result;
int word_count;
int i;
int start;
if (!s)
return (NULL);
word_count = count_words(s, c);
result = (char **)malloc((word_count + 1) * sizeof(char *));
if (!result)
return (NULL);
i = 0;
start = 0;
while (i < word_count)
{
result[i] = get_next_word(s, c, &start);
if (!result[i])
{
free_result(result);
}
i++;
}
result[i] = NULL;
return (result);
}