-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
122 lines (106 loc) · 3.39 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
111
112
113
114
115
116
117
118
119
120
121
122
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amait-ou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/01 23:14:20 by amait-ou #+# #+# */
/* Updated: 2023/06/27 18:16:42 by amait-ou ### ########.fr */
/* */
/* ************************************************************************** */
/*
The "ft_split" function returns an array of strings split according to
a particular character.
The first function I use is "words(const char *s, char c)" which counts
how many words are within a "*s". its main idea is looping over the string
and start positioning the pointer step ahead as long as the pointer is equal
to the character, once a character doesn't equal the pointer, then this is the
first element of the word. and we know the end of a word when we make
the pointer to step ahead to equal the character. once this condition is true,
a variable counter will be incremented by one.
The second function I use is "letters(const char *s, int position, char c)"
generally, it counts how many letters are contained within a word according to
the position of the word as it starts moving the pointer straight ahead and
increments the pointer by one as long as the pointer is not equal to the "c".
Thirdly, "allocation(char **p, char const *s, int i, char c)", basically
this function allocates a block of memory for each word according to how
many letters it has got using the returned result from the "letter" function
in a double pointer that refers to a 2D array where we store our data.
finally, our hero function which is right here "ft_split(const char *s), int c"
that allocates a double pointer to help us store our split strings using the
"words" function that counts the number of words.
note: if the allocation fails in any piece of code. "NULL" will be returned.
*/
#include "libft.h"
static int words(const char *s, char c)
{
int i;
char *p;
i = 0;
p = (char *)s;
while (*p)
{
while (*p == c)
++p;
if (*p)
{
while (*p && *p != c)
++p;
++i;
}
}
return (i);
}
static int letters(const char *s, int position, char c)
{
int i;
char *p;
p = (char *)s;
i = 0;
while (*(p + position) && *(p + position) != c)
{
++i;
++position;
}
return (i);
}
static char **allocation(char **p, char const *s, int i, char c)
{
int k;
int j;
j = 0;
while (s[i])
{
k = 0;
while (s[i] && s[i] == c)
++i;
if (s[i])
{
p[j] = (char *)malloc(sizeof(char) * (letters(s, i, c) + 1));
while (s[i] && s[i] != c)
{
if (!p[j])
return ((void *)0);
p[j][k++] = s[i];
++i;
}
p[j++][k] = '\0';
}
}
p[j] = (void *)0;
return (p);
}
char **ft_split(char const *s, char c)
{
char **p;
int i;
i = 0;
if (!s)
return ((void *)0);
p = (char **)malloc(sizeof(char *) * (words(s, c) + 1));
if (!p)
return ((void *)0);
allocation(p, s, i, c);
return (p);
}