-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
95 lines (86 loc) · 2.16 KB
/
ft_split.c
File metadata and controls
95 lines (86 loc) · 2.16 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
███▄ ▄███▓▓█████ ███▄ █ ▄▄▄ ██████▓██ ██▓
▓██▒▀█▀ ██▒▓█ ▀ ██ ▀█ █ ▒████▄ ▒██ ▒ ▒██ ██▒
▓██ ▓██░▒███ ▓██ ▀█ ██▒▒██ ▀█▄ ░ ▓██▄ ▒██ ██░
▒██ ▒██ ▒▓█ ▄ ▓██▒ ▐▌██▒░██▄▄▄▄██ ▒ ██▒ ░ ▐██▓░
▒██▒ ░██▒░▒████▒▒██░ ▓██░ ▓█ ▓██▒▒██████▒▒ ░ ██▒▓░
░ ▒░ ░ ░░░ ▒░ ░░ ▒░ ▒ ▒ ▒▒ ▓▒█░▒ ▒▓▒ ▒ ░ ██▒▒▒
░ ░ ░ ░ ░ ░░ ░░ ░ ▒░ ▒ ▒▒ ░░ ░▒ ░ ░▓██ ░▒░
░ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ▒ ░░
░ ░ ░ ░ ░ ░ ░ ░ ░
░ ░*/
#include "libft.h"
static int ft_word(const char *s, char c)
{
int i;
int sayac;
sayac = 0;
i = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != c && s[i] != '\0')
{
i++;
sayac++;
}
while (s[i] != c && s[i])
i++;
}
return (sayac);
}
static int ft_wordlen(char const *s, char c, int start)
{
int i;
i = 0;
while (s[start] != c && s[start])
{
start++;
i++;
}
return (i);
}
static char *ft_atama(char const *s, char c, int start)
{
int i;
int j;
char *dest;
j = 0;
i = start;
dest = malloc(sizeof(char) * (ft_wordlen(s, c, start) + 1));
if (!dest)
return (0);
while (s[i] && s[i] != c)
{
dest[j] = s[i];
i++;
j++;
}
dest[j] = '\0';
return (dest);
}
char **ft_split(char const *s, char c)
{
int i;
int j;
char **dest;
i = 0;
j = -1;
if (!s)
return (0);
dest = malloc(sizeof(char *) * (ft_word(s, c) + 1));
if (!dest)
return (0);
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i] != '\0')
dest[++j] = ft_atama(s, c, i);
while (s[i] != c && s[i])
i++;
}
dest[++j] = NULL;
return (dest);
}