-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
110 lines (102 loc) · 2.21 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
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vess <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/14 18:16:54 by vess #+# #+# */
/* Updated: 2021/11/28 12:04:01 by jcampagn ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static char **ft_panic(char **arr, int len)
{
while (--len >= 0)
{
free(arr[len]);
}
free(arr);
return (NULL);
}
static int ft_count_words(const char *str, char c)
{
int count;
int i;
i = 0;
count = 0;
if (!str)
return (0);
while (str[i])
{
while (str[i] && str[i] == c)
i++;
if (str[i] && str[i] != c)
{
count++;
while (str[i] && str[i] != c)
i++;
}
}
return (count);
}
static char *malloc_word(const char *str, char c)
{
char *word;
int i;
i = 0;
while (str[i] && str[i] != c)
i++;
word = malloc(sizeof(char) * (i + 1));
if (!word)
return (NULL);
i = 0;
while (str[i] && str[i] != c)
{
word[i] = str[i];
i++;
}
word[i] = 0;
return (word);
}
char **ft_split(char const *s, char c)
{
int i;
char **arr;
i = 0;
arr = 0;
if (!s)
return (0);
arr = (char **)malloc(sizeof(char *) *(ft_count_words(s, c) + 1));
if (!arr)
return (NULL);
while (*s)
{
while (*s && *s == c)
s++;
if (*s && *s != c)
{
arr[i] = malloc_word(s, c);
if (!arr[i++])
return (ft_panic(arr, i - 1));
while (*s && *s != c)
s++;
}
}
arr[i] = 0;
return (arr);
}
/*
#include <stdio.h>
int main ()
{
char **arr;
char *str = "**Hello*samere*lapute**";
arr = ft_split(str, '*');
printf("%s\n",arr[0]);
printf("%s\n",arr[1]);
printf("%s\n",arr[2]);
return (0);
}
*/