-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
102 lines (93 loc) · 1.43 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
#include "sort_stack.h"
char *ft_strdup(const char *s1)
{
char *dup;
int len;
int i;
len = ft_strlen(s1);
dup = malloc((len + 1) * sizeof(char));
i = 0;
if (!dup)
return (NULL);
while (s1[i])
{
dup[i] = s1[i];
i++;
}
dup[i] = '\0';
return (dup);
}
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (unsigned char) c)
return ((char *)s);
s++;
}
if (*s == (unsigned char)c)
return ((char *)s);
return (NULL);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
int i;
size_t strlen;
i = 0;
if (!s)
return (NULL);
strlen = ft_strlen(s);
if (len > strlen)
len = strlen - start;
if (start > strlen)
return (ft_strdup(""));
sub = malloc((len + 1) * sizeof(char));
if (!sub)
return (NULL);
while (s[start] && len--)
{
sub[i] = s[start];
start++;
i++;
}
sub[i] = '\0';
return (sub);
}
int wordcount(const char *s, char c)
{
int count;
count = 0;
while (s && *s)
{
while (*s == c)
s++;
count += *s != 0;
s = ft_strchr(s, c);
}
return (count);
}
char **ft_split(char const *s, char c)
{
int words;
char **result;
int i;
size_t len;
words = wordcount(s, c);
result = malloc((words + 1) * sizeof(char *));
if (!result)
return (0);
i = 0;
while (i < words)
{
while (*s == c)
s++;
len = ft_strchr(s, c) - s;
result[i] = ft_substr(s, 0, len);
s += len;
if (!result[i++])
return (0);
}
result[i] = 0;
return (result);
}