-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtow.c
78 lines (70 loc) · 1.14 KB
/
strtow.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
#include "main.h"
#include <stddef.h>
/**
* strtow - split a string into tokens by delimeter
*
* @str: string
* @del: delimeter
*
* Return: array of strings of tokens
**/
char **strtow(const char *str, const char del)
{
char **s = NULL;
int i = 0, j, w = 0;
while (str[i] != '\0' && str[i] == del)
i++;
if (!str[i])
return (NULL);
i = 0;
while (str[i])
{
j = i;
if (str[i] != del)
{
s = _realloc(s, sizeof(char *) * w,
sizeof(char *) * (w + 1));
s[w] = NULL;
while (str[j] && str[j] != del)
{
s[w] = _realloc(s[w], (j - i), ((j - i) + 1));
s[w][j - i] = str[j];
j++;
}
s[w] = _realloc(s[w], (j - i), ((j - i) + 1));
s[w][j - i] = '\0';
i += (j - i);
w++;
}
else
i++;
}
s = _realloc(s, sizeof(char *) * w, sizeof(char *) * (w + 1));
s[w] = NULL;
return (s);
}
/**
* free_tow - free the array
* @tow: array
*
**/
void free_tow(char **tow)
{
int i = 0;
while (tow[i])
free(tow[i++]);
free(tow);
}
/**
* len_tow - number of elements in tow array
* @av: array
*
* Return: length of array
**/
size_t len_tow(char **av)
{
size_t i = 0;
while (av[i])
i++;
return (i);
}