- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
Latest commit
92 lines (83 loc) · 1.95 KB
/
Copy pathft_strsplit.c
File metadata and controls
92 lines (83 loc) · 1.95 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mnishimo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/26 23:43:30 by mnishimo #+# #+# */
/* Updated: 2019/03/30 13:47:35 by mnishimo ### ########.fr */
/* */
/* ************************************************************************** */
#include"libftprintf.h"
staticintcountwords(charconst*s, charc)
{
inti;
intwc;
if (s==NULL||s[0] =='\0')
return (0);
i=0;
wc=0;
if (s[i] !=c)
wc++;
while (s[i] !='\0')
{
if (s[i] ==c&&s[i+1] !=c&&s[i+1] !='\0')
wc++;
i++;
}
return (wc);
}
staticchar*trim(charconst*s, charc, int*start)
{
intj;
char*ptr;
inti;
i=*start;
while (s[i] !='\0'&&s[i] ==c)
i++;
j=i;
while (s[j] !='\0'&&s[j] !=c)
j++;
ptr=ft_strsub(s, i, j-i);
if (ptr==NULL)
return (NULL);
*start=i;
return (ptr);
}
staticvoiddelete(char**s, intindex)
{
inti;
i=0;
while (i<index)
{
free(s[i]);
i++;
}
free(s);
}
char**ft_strsplit(charconst*s, charc)
{
intwc;
char**ret;
inti;
intj;
wc=countwords(s, c);
if (!(ret= (char**)malloc(sizeof(char*) * (wc+1))))
return (NULL);
i=-1;
j=0;
while (i++<wc-1)
{
ret[i] =trim(s, c, &j);
if (ret[i] ==NULL)
{
delete(ret, i);
return (NULL);
}
while (s[j] !=c&&s[j] !='\0')
j++;
}
ret[i] =NULL;
return (ret);
}