-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
87 lines (79 loc) · 1.84 KB
/
ft_split.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_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: crigonza <crigonza@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/24 19:22:55 by crigonza #+# #+# */
/* Updated: 2022/05/05 17:21:53 by crigonza ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_wcounter(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
if (s[i] != c)
{
i++;
count++;
}
while (s[i] != '\0')
{
if (s[i] == c && s[i + 1] != c && s[i + 1] != '\0')
count++;
i++;
}
return (count);
}
char **ft_freestr(char **str, int i)
{
while (i >= 0)
{
free(str[i]);
str[i] = NULL;
i--;
}
free(str);
str = NULL;
return (NULL);
}
char **ft_addsubstr(char **str, char const *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (*s)
{
if (*s != c)
{
j = 0;
while (s[j] && s[j] != c)
j++;
str[i] = ft_substr(s, 0, j);
if (!str[i])
return (ft_freestr(str, i));
i++;
s = s + j;
}
else
s++;
}
str[i] = NULL;
return (str);
}
char **ft_split(char const *s, char c)
{
char **splitstr;
if (!s)
return (NULL);
splitstr = (char **)malloc(sizeof(char *) * (ft_wcounter(s, c) + 1));
if (!splitstr)
return (NULL);
splitstr = ft_addsubstr(splitstr, s, c);
return (splitstr);
}