-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
139 lines (125 loc) · 3.29 KB
/
Copy pathft_split.c
File metadata and controls
139 lines (125 loc) · 3.29 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atok <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 13:49:06 by atok #+# #+# */
/* Updated: 2022/10/27 13:01:43 by atok ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int word_count(const char *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i] != 0x00)
{
while (s[i] == c && s[i] != 0x00)
i++;
if (s[i] != 0x00)
count++;
while (s[i] != c && s[i] != 0x00)
i++;
}
return (count);
}
char *word_dup(const char *s, char c)
{
int len;
int i;
char *word;
i = 0;
len = 0;
while (s[len] != 0x00 && s[len] != c)
len++;
word = (char *) malloc(sizeof(char) * (len + 1));
if (word == NULL)
return (NULL);
while (i < len)
{
word[i] = s[i];
i++;
}
word[i] = 0x00;
return (word);
}
char **ft_split(char const *s, char c)
{
char **split;
int i;
int j;
if (s == NULL)
return (NULL);
i = 0;
j = 0;
split = (char **)malloc(sizeof(char *) * (word_count(s, c) + 1));
if (split == NULL)
return (NULL);
while (s[i] != 0x00)
{
while (s[i] == c && s[i] != 0x00)
i++;
if (s[i] != 0x00)
{
split[j] = word_dup(&s[i], c);
j++;
}
while (s[i] != c && s[i] != 0x00)
i++;
}
split[j] = 0x00;
return (split);
}
/* int main(void)
{
char **split;
size_t i = 0;
size_t j = 0;
char str[] = " hello world 123 test ";
char delim = ' ';
split = ft_split(str, delim);
printf("andi str has %i words\n", word_count(str, delim));
// printf("%s\n", *ft_split(str, delim)); // print first array
while (split[i] != 0x00)
{
printf("%s\n", split[i]);
i++;
}
*/
// while (split[i] != 0x00)
// {
// j = 0;
// while (split[i][j])
// write(1, &split[i][j++], 1);
// write(1, "\n", 1);
// i++;
// }
// return (0);
// }
/* **ft_.. = 2D array meaning
from this s [] ="Hi my name is"
to below
s [1] = "Hi"
s [2] = "my"
s [3] = "name"
s [4] = "is"
s [[1][2][3][4]] = "Hi" "my" "name" "is"
witin 1 array there is multiple array
can split how ever many time as long as there is delimiter
which is specified by user
might need to while loop and print to see it */
/* word_count MUST be be in that sequence in order handle
if delimiter at the front and back OR only at the front OR only at the back
does not wokr in any other way.. if it can handle the front,
it will not be able to handle the back & vice versa..
BECAUSE
if there is no delimiter it will count++ on the 1st word
skipping the delimiter checking
if there is delimiter it will loop pass the delimiter then count++
in other workds, count++ must be before a non-delimiter
cmust ount++ FIRST on every first letter word
if count++ after a word, NULL tirminal will fuck it up*/