-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
81 lines (74 loc) · 2.01 KB
/
Copy pathft_split.c
File metadata and controls
81 lines (74 loc) · 2.01 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nino <nino@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 13:14:12 by nino #+# #+# */
/* Updated: 2021/01/20 08:19:28 by nino ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static char **freesplit(char **split)
{
int i;
i = 0;
while (split[i])
{
free(split[i]);
i++;
}
free(split);
return (NULL);
}
static int countwl(char const *str, char c, int w_or_l)
{
int words;
int letters;
char *ptr_str;
words = 0;
if (!str)
return (words);
ptr_str = (char *)str;
while (*ptr_str)
{
letters = 0;
while (*ptr_str == c)
ptr_str++;
while (*ptr_str && *ptr_str++ != c)
letters++;
words = (letters) ? (words + 1) : words;
if (w_or_l == 1)
return (letters);
}
return (words);
}
char **ft_split(char const *str, char c)
{
int i;
int j;
int words;
char **str_return;
i = 0;
j = 0;
words = 0;
if (!str)
return (NULL);
words = countwl(str, c, 0);
if (!(str_return = (char **)malloc(sizeof(char *) * (words + 1))))
return (NULL);
str_return[words] = NULL;
while (words--)
{
while ((char)str[j] == c)
j++;
if (!(str_return[i] = (char *)malloc(countwl(&str[j], c, 1) + 1)))
return (freesplit(str_return));
ft_strlcpy(str_return[i], &str[j], countwl(&str[j], c, 1) + 1);
j += countwl(&str[j], c, 1);
i++;
}
return (&*str_return);
}