-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
75 lines (68 loc) · 1.77 KB
/
split.c
File metadata and controls
75 lines (68 loc) · 1.77 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kikiz <kikiz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/23 19:19:18 by kikiz #+# #+# */
/* Updated: 2025/03/25 05:33:32 by kikiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int count_words(char *str, char c)
{
int i;
int trigger;
i = 0;
trigger = 0;
while (*str)
{
if (*str != c && trigger == 0)
{
trigger = 1;
i++;
}
else if (*str == c)
trigger = 0;
str++;
}
return (i);
}
char *word_dup(char *str, int start, int finish)
{
char *word;
int i;
i = 0;
word = malloc((finish - start + 1) * sizeof(char));
while (start < finish)
word[i++] = str[start++];
word[i] = '\0';
return (word);
}
char **ft_split(char *s, char c)
{
char **split;
int index;
int i;
int j;
split = malloc((count_words(s, c) + 1) * sizeof(char *));
if (!s || !split)
return (0);
i = 0;
j = 0;
index = -1;
while (i <= ft_strlen(s))
{
if (s[i] != c && index < 0)
index = i;
else if ((s[i] == c || i == ft_strlen(s)) && index >= 0)
{
split[j++] = word_dup(s, index, i);
index = -1;
}
i++;
}
split[j] = 0;
return (split);
}