-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
87 lines (79 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: harslan <harslan@student.42istanbul.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/13 17:07:38 by harslan #+# #+# */
/* Updated: 2022/09/08 03:18:08 by harslan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int seperator(char s, char c)
{
if (s == c)
return (1);
return (0);
}
static int counter(char *str, char c)
{
int i;
int count;
count = 0;
i = 0;
while (str[i])
{
while (seperator(str[i], c))
i++;
if (!seperator(str[i], c) && str[i])
count++;
while (!seperator(str[i], c) && str[i])
i++;
}
return (count);
}
static char *worddup(char *s, char c)
{
char *word;
int i;
i = 0;
while (s[i] && !seperator(s[i], c))
i++;
word = malloc(sizeof(char) * (i + 1));
i = 0;
while (s[i] && !seperator(s[i], c))
{
word[i] = s[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
int i;
char **strarray;
char *str;
int count;
str = (char *)s;
i = 0;
count = counter(str, c);
strarray = (char **)malloc(sizeof(char *) * (count + 1));
if (!strarray)
return (NULL);
while (*str)
{
while (*str && seperator(*str, c))
str++;
if (*str)
{
strarray[i] = worddup(str, c);
i++;
while (*str && !seperator(*str, c))
str++;
}
}
strarray[i] = NULL;
return (strarray);
}