-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
70 lines (63 loc) · 1.72 KB
/
ft_strsplit.c
File metadata and controls
70 lines (63 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mle-roy <mle-roy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/25 13:08:55 by mle-roy #+# #+# */
/* Updated: 2015/01/31 21:39:10 by mle-roy ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static size_t ft_strlenws(char const *s, char c)
{
size_t result;
result = 0;
while (*s != c && *s)
{
result++;
s++;
}
return (result);
}
static char **ft_spliting(char const *s, char **newt, char c)
{
unsigned int i;
int x;
i = 0;
x = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
newt[x++] = ft_strsub(s, i, ft_strlenws(&s[i], c));
i = i + ft_strlenws(&s[i], c);
}
else
i++;
}
return (newt);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int t;
char **newtab;
i = 0;
t = 0;
while (s[i] != '\0')
{
if (s[i] != c && ((i != 0 && s[i - 1] == c) || i == 0))
t++;
i++;
}
newtab = (char**)(malloc(sizeof(newtab) * (t + 1)));
if (newtab == NULL)
return (NULL);
while (t != -1)
newtab[t--] = '\0';
newtab = ft_spliting(s, newtab, c);
return (newtab);
}