-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
77 lines (70 loc) · 1.73 KB
/
ft_split.c
File metadata and controls
77 lines (70 loc) · 1.73 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsousa-d <bsousa-d@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 16:54:46 by bsousa-d #+# #+# */
/* Updated: 2023/10/09 15:41:31 by bsousa-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t word_count(char const *s, char c)
{
size_t i;
size_t j;
size_t flag;
i = 0;
j = 0;
flag = 1;
while (s[i])
{
if (s[i] != c && flag)
{
flag = 0;
j++;
}
else if (s[i] == c)
flag = 1;
i++;
}
return (j);
}
static size_t letters_in_word(char const *s, char c, size_t i)
{
size_t size;
size = 0;
while (s[i] && s[i] != c)
{
size++;
i++;
}
return (size);
}
char **ft_split(char const *s, char c)
{
size_t i;
size_t j;
size_t words;
char **arr;
if (!s)
return (NULL);
j = 0;
i = 0;
words = word_count(s, c);
arr = (char **)malloc(sizeof(char *) * (words + 1));
if (!arr)
return (NULL);
while (j < words)
{
while (s[i] == c)
i++;
arr[j] = ft_substr(s, i, letters_in_word(s, c, i));
if (!arr[j++])
return (NULL);
i += letters_in_word(s, c, i);
}
arr[j] = NULL;
return (arr);
}