-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_strsplit.c
More file actions
83 lines (76 loc) · 1.82 KB
/
ft_strsplit.c
File metadata and controls
83 lines (76 loc) · 1.82 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akassil <akassil@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/30 22:33:37 by akassil #+# #+# */
/* Updated: 2018/05/02 20:09:00 by akassil ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int length(char *str, char c)
{
int i;
i = 0;
while ((*str != c) && *str)
{
str++;
i++;
}
return (i);
}
static int get_words(char *str, char c)
{
int i;
int chr;
i = 0;
chr = 0;
if (!str)
return (0);
while (*str)
{
if ((*str) == c)
{
str++;
chr = 0;
}
else
{
if (chr == 0)
i++;
chr = 1;
str++;
}
}
return (i);
}
char **ft_strsplit(char const *s, char c)
{
int words;
int word_number;
char **result;
char *word;
int i;
i = 0;
word_number = 0;
words = get_words((char *)s, c);
result = (char **)malloc(sizeof(char *) * (words + 1));
if (!result || !s)
return (NULL);
while (words > word_number)
{
while (*s == c)
s++;
word = (char *)malloc(sizeof(char) * (length((char *)s, c) + 1));
while (!(*s == c) && *s)
word[i++] = *s++;
word[i] = '\0';
result[word_number++] = word;
i = 0;
}
result[word_number] = 0;
return (result);
}