-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
75 lines (67 loc) · 1.77 KB
/
ft_split.c
File metadata and controls
75 lines (67 loc) · 1.77 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnoufel <bnoufel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 16:04:47 by bnoufel #+# #+# */
/* Updated: 2020/02/08 19:49:38 by bnoufel ### ########.fr */
/* */
/* ************************************************************************** */
#include "str.h"
#include "tab.h"
/*
** ft_split split a strings with the char c delimiter to a tab was allocate.
** @param str
** @param c
** @return NULL if allocate failed or return the new tab
*/
static size_t ft_count_words(char const *s, char c)
{
size_t words;
size_t i;
words = 0;
i = 0;
while (s[i++])
{
if (s[i] == c)
continue ;
words++;
while (s[i] && s[i] != c)
i++;
}
return (words);
}
char **split(const char *str, char c, char **tab)
{
size_t j;
size_t i;
size_t len;
j = 0;
i = 0;
while (str[i])
{
if (str[i] != c)
{
len = ft_strlen_c(str + i, c);
tab[j] = ft_strsub(str + i, 0, len);
if (!tab[j])
return (NULL);
i += len;
j++;
}
else
i++;
}
tab[j] = 0;
return (tab);
}
char **ft_split(const char *str, char c)
{
char **tab;
tab = ft_tabnew(ft_count_words(str, c) + 1);
if (!tab)
return (NULL);
return (split(str, c, tab));
}