-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
91 lines (82 loc) · 1.85 KB
/
ft_split.c
File metadata and controls
91 lines (82 loc) · 1.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jihyjeon < jihyjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/19 18:43:18 by jihyjeon #+# #+# */
/* Updated: 2023/11/09 14:46:59 by jihyjeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t s_cnt(const char *str, char chr)
{
size_t num;
num = 0;
while (*str)
{
while (*str && (*str == chr))
str++;
if (*str)
num++;
while (*str && !(*str == chr))
str++;
}
return (num);
}
size_t s_len(const char *str, char c)
{
size_t i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (i);
}
char *c_arr(const char *str, char c)
{
size_t i;
size_t len;
char *new;
len = s_len(str, c);
new = (char *)malloc(sizeof(char) * (len + 1));
if (!new)
return (0);
i = 0;
while (i < len)
{
new[i] = str[i];
i++;
}
new[i] = 0;
return (new);
}
char **ft_split(char const *s, char c)
{
char **arr;
size_t i;
size_t nos;
nos = s_cnt(s, c);
arr = (char **)malloc(sizeof(char *) * (nos + 1));
if (!(arr))
return (0);
i = 0;
while (*s != 0)
{
while (*s != 0 && (*s == c))
s++;
if (*s != 0)
{
arr[i] = c_arr(s, c);
i++;
}
while (*s != 0 && !(*s == c))
s++;
}
arr[i] = 0;
return (arr);
}