-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
87 lines (78 loc) · 1.9 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
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alalaoui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/20 15:38:32 by alalaoui #+# #+# */
/* Updated: 2017/10/23 17:44:39 by alalaoui ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <string.h>
#include <stdlib.h>
static size_t ft_words(char const *s, char c)
{
size_t i;
size_t words;
int flag;
flag = 0;
words = 0;
i = 0;
if (!s)
return (0);
while (s[i])
{
if (s[i] != c && flag == 0)
{
words++;
flag = 1;
}
if (s[i] == c && flag == 1)
flag = 0;
i++;
}
return (words);
}
static size_t ft_strlenc(char const *s, char c)
{
size_t len;
len = 0;
while (s[len] && s[len] != c)
len++;
return (len);
}
int ft_split_len(char **split)
{
int len;
len = 0;
while (split[len])
len++;
return (len);
}
char **ft_strsplit(char const *s, char c)
{
char **fresh;
size_t i;
size_t len;
i = 0;
len = 0;
if (!s || !(fresh = (char **)malloc(sizeof(char*) * (ft_words(s, c) + 1))))
return (NULL);
fresh[ft_words(s, c)] = 0;
while (*s)
{
while (*s != '\0' && *s == c)
s++;
if (*s != '\0' && *s != c)
{
len = ft_strlenc(s, c);
fresh[i] = ft_strsub(s, 0, len);
i++;
}
while (*s != '\0' && *s != c)
s++;
}
return (fresh);
}