blob: 7c8e5c7c9759f1ec9fecc6fd889747f813184daf (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/12 18:35:38 by exam #+# #+# */
/* Updated: 2019/07/12 19:27:00 by exam ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#define ISSEP(c) (c == ' ' || c == '\n' || c == '\t')
int count_segment(char *str)
{
int counter;
counter = 0;
while (*str)
{
if (!ISSEP(*str))
{
counter++;
while (*str && !ISSEP(*str))
str++;
if (!*str)
break;
}
str++;
}
return (counter);
}
char **ft_split(char *str)
{
char **split;
char *tmp;
int i;
int j;
int segments;
segments = count_segment(str);
split = (char**)malloc(sizeof(char*) * segments + 1);
if (split == NULL)
return (NULL);
j = 0;
while (j < segments)
{
if (ISSEP(*str))
{
str++;
continue;
}
i = 0;
while (!ISSEP(str[i]))
i++;
tmp = (char*)malloc(sizeof(char) * i + 1);
if (tmp == NULL)
return (NULL);
i = 0;
while (!ISSEP(*str))
tmp[i++] = *str++;
tmp[i] = '\0';
split[j] = tmp;
j++;
}
split[j] = NULL;
return (split);
}
|