-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
75 lines (68 loc) · 1.77 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibohonos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 16:03:27 by ibohonos #+# #+# */
/* Updated: 2017/11/07 11:56:14 by ibohonos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i] != '\0')
{
if (s[i] == c && s[i + 1] != c)
j++;
i++;
}
if (s[0] != '\0')
j++;
return (j);
}
static char *ft_word(const char *str, char c, int *i)
{
char *s;
int j;
s = (char *)malloc(sizeof(char) * ft_strlen(str));
if (s == NULL)
return (NULL);
j = 0;
while (str[*i] != c && str[*i])
{
s[j] = str[*i];
j++;
*i += 1;
}
s[j] = '\0';
while (str[*i] == c && str[*i])
*i += 1;
return (s);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int j;
int w;
char **str;
if (!s)
return (NULL);
i = 0;
j = 0;
w = ft_count_words(s, c);
str = (char **)malloc(sizeof(char **) * (w + 2));
if (str == NULL)
return (NULL);
while (s[i] == c && s[i] != '\0')
i++;
while (j < w && s[i] != '\0')
str[j++] = ft_word(s, c, &i);
str[j] = NULL;
return (str);
}