-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
76 lines (69 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ademenet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/26 12:03:50 by ademenet #+# #+# */
/* Updated: 2015/12/16 15:27:42 by ademenet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include "libft.h"
static int ft_nbofwords(char *s, char c)
{
int i;
int is_word;
i = 0;
is_word = 0;
while (*s)
{
if (is_word == 0 && *s != c)
{
is_word = 1;
i++;
}
else if (is_word == 1 && *s == c)
is_word = 0;
s++;
}
return (i);
}
static int ft_strlenlim(char *s, char c)
{
int len;
len = 0;
while (*s != c && *s != '\0')
{
len++;
s++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
int nb_ofwords;
char **tab;
int i;
if (!s)
return (NULL);
nb_ofwords = ft_nbofwords((char *)s, c);
tab = (char **)malloc((nb_ofwords + 1) * sizeof(char*));
i = 0;
if (!tab)
return (NULL);
while (nb_ofwords--)
{
while (*s == c && *s != '\0')
s++;
tab[i] = ft_strsub((char *)s, 0, ft_strlenlim((char *)s, c));
if (!tab[i])
return (NULL);
s = s + ft_strlenlim((char *)s, c);
i++;
}
tab[i] = NULL;
return (tab);
}