-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
116 lines (105 loc) · 2.45 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: javellis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 18:15:03 by javellis #+# #+# */
/* Updated: 2022/10/12 18:15:06 by javellis ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include"libft.h"
static void ft_freestrs(char *strs)
{
int i;
i = 0;
while (strs[i] != '\0')
{
free(strs);
i++;
}
free(strs);
}
static int ft_wordcounter(char *str, char sep)
{
int i;
int word;
i = 0;
word = 0;
while (str[i] != '\0')
{
if (str[i] != sep)
{
word++;
while (str[i] != sep && str[i] != '\0')
i++;
if (str[i] == '\0')
return (word);
}
i++;
}
return (word);
}
static void ft_strcpy(char *word, char *str, char sep, int j)
{
int i;
i = 0;
while (str[j] != '\0' && str[j] == sep)
j++;
while (str[j + i] != sep && str[j + i] != '\0')
{
word[i] = str[j + i];
i++;
}
word[i] = '\0';
}
static char *ft_stralloc(char *str, char sep, int *k)
{
char *word;
int j;
j = *k;
word = NULL;
while (str[*k] != '\0')
{
if (str[*k] != sep)
{
while (str[*k] != '\0' && str[*k] != sep)
*k += 1;
word = (char *)malloc(sizeof(char) * (*k + 1));
if (word == NULL)
return (NULL);
break ;
}
*k += 1;
}
ft_strcpy(word, str, sep, j);
return (word);
}
char **ft_split(char const *str, char sep)
{
char **strs;
int i;
int j;
int pos;
if (str == NULL)
return (NULL);
i = 0;
pos = 0;
j = ft_wordcounter((char *)str, sep);
strs = (char **)malloc(sizeof(char *) * (j + 1));
if (strs == NULL)
return (NULL);
strs[j] = NULL;
while (i < j)
{
strs[i] = ft_stralloc(((char *)str), sep, &pos);
if (strs[i] == NULL)
{
ft_freestrs(strs[i]);
}
i++;
}
return (strs);
}