-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
106 lines (94 loc) · 2.36 KB
/
ft_split.c
File metadata and controls
106 lines (94 loc) · 2.36 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tkankan <tkankan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/23 18:35:33 by tkankan #+# #+# */
/* Updated: 2022/03/01 15:58:41 by tkankan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *s, char c)
{
int index;
int count;
index = 0;
count = 0;
while (s[index])
{
if (s[index] == c)
index++;
else
{
count++;
while (s[index] && s[index] != c)
index++;
}
}
return (count);
}
static char *ft_make_words(char *word, char const *s, int j, int word_ln)
{
int index;
index = 0;
while (word_ln > 0)
word[index++] = s[j - word_ln--];
word[index] = 0;
return (word);
}
static char **ft_split_words(char **res, char const *s, char c, int word_ct)
{
int s_index;
int res_index;
int word_ln;
res_index = 0;
s_index = 0;
word_ln = 0;
while (s[s_index] && res_index < word_ct)
{
while (s[s_index] && s[s_index] == c)
s_index++;
while (s[s_index] && s[s_index] != c)
{
s_index++;
word_ln++;
}
res[res_index] = (char *)malloc(sizeof(char) * (word_ln + 1));
if (!res[res_index])
return (NULL);
ft_make_words (res[res_index], s, s_index, word_ln);
word_ln = 0;
res_index++;
}
res[res_index] = 0;
return (res);
}
char **ft_split(char const *s, char c)
{
int word_ct;
char **res;
if (s == NULL)
return (NULL);
word_ct = ft_count_words(s, c);
res = (char **)malloc(sizeof(char *) * (word_ct + 1));
if (!res)
return (NULL);
ft_split_words (res, s, c, word_ct);
return (res);
}
/*
#include "stdio.h"
int main(void)
{
int i = 0;
char **tab;
tab = ft_split("Cagri, abim, libft, meister", ',');
while (i < 4)
{
printf("string %d : %s\n", i, tab[i]);
i++;
}
return (0);
}*/