-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
79 lines (72 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rjobert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/12 17:35:05 by rjobert #+# #+# */
/* Updated: 2023/07/12 17:35:07 by rjobert ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_wordcount(const char *s, char c)
{
int count;
int i;
count = 0;
i = 0;
while (s[i])
{
if (i > 0 && s[i] == c && s[i - 1] != c && s[i + 1] != 0)
count++;
i++;
}
return (count + 1);
}
static char *word_insert(const char *str, char c)
{
int i;
char *word;
i = 0;
while (str[i] && str[i] != c)
i++;
word = malloc(i + 1);
if (!word)
return (NULL);
i = 0;
while (str[i] && str[i] != c)
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
char **matrix;
int i;
int wc;
wc = ft_wordcount(s, c);
if (!s)
return (NULL);
matrix = malloc((wc + 1) * 8);
if (!matrix)
return (NULL);
i = 0;
while (*s)
{
while (*s && *s == c)
s++;
if (*s && *s != c)
{
matrix[i] = word_insert(s, c);
i++;
while (*s && *s != c)
s++;
}
}
matrix[i] = NULL;
return (matrix);
}