-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
90 lines (83 loc) · 2.17 KB
/
ft_split.c
File metadata and controls
90 lines (83 loc) · 2.17 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgomes-n < lgomes-n@student.42sp.org.br +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/18 21:00:10 by lgomes-n #+# #+# */
/* Updated: 2023/05/18 21:00:10 by lgomes-n ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *s, char c)
{
int count;
int i;
count = 0;
i = 0;
if (s[i] == '\0')
return (0);
while (s[i] != '\0')
{
if (s[i] != c)
{
if ((s[i + 1] == c) || (s[i + 1] == '\0'))
count++;
}
i++;
}
return (count);
}
static char *ft_get_next_word(char const **s, char c)
{
char *word;
int length;
int i;
int j;
length = 0;
i = 0;
while ((*s)[i] != '\0' && (*s)[i] == c)
i++;
while ((*s)[i + length] != '\0' && (*s)[i + length] != c)
length++;
word = (char *)malloc((length + 1) * sizeof(char));
if (word == NULL)
return (NULL);
j = 0;
while (j < length)
{
word[j] = (*s)[i + j];
j++;
}
word[j] = '\0';
*s = *s + (i + length);
return (word);
}
char **ft_split(char const *s, char c)
{
int word_count;
char **result;
int i;
if (s == NULL)
return (NULL);
word_count = ft_count_words(s, c);
result = (char **)malloc((word_count + 1) * sizeof(char *));
if (result == NULL)
return (NULL);
i = 0;
while (i < word_count)
{
result[i] = ft_get_next_word(&s, c);
if (result[i] == NULL)
{
while (i > 0)
free(result[--i]);
free(result[i]);
return (NULL);
}
i++;
}
result[word_count] = NULL;
return (result);
}