-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
84 lines (76 loc) · 1.87 KB
/
ft_split.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nwatanab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/07 16:28:25 by nwatanab #+# #+# */
/* Updated: 2020/11/23 18:40:23 by nwatanab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int count_words(char const *s, char c)
{
int i;
int words;
if (s == NULL)
return (0);
i = 0;
words = 0;
while (s[i])
{
if (words == 0 && s[i] != c)
words++;
else if (s[i] != c && s[i - 1] == c)
words++;
i++;
}
return (words);
}
int word_len(char const *s, char c)
{
int len;
len = 0;
while (*s == c)
s++;
while (*s != c && *s)
{
s++;
len++;
}
return (len);
}
char **free_all(char **str, int i)
{
while (i > 0)
free(str[--i]);
free(str);
return (NULL);
}
char **ft_split(char const *s, char c)
{
int i;
int j;
int words_count;
char **str;
words_count = count_words(s, c);
if (!s || !(str = malloc(sizeof(char *) * (words_count + 1))))
return (NULL);
i = 0;
while (i < words_count)
{
str[i] = malloc(sizeof(char) * (word_len(s, c) + 1));
if (str[i] == NULL)
return (free_all(str, i));
while (*s == c)
s++;
j = 0;
while (*s != c && *s)
str[i][j++] = *s++;
str[i][j] = '\0';
i++;
}
str[i] = NULL;
return (str);
}