-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strtok.c
More file actions
70 lines (60 loc) · 1.27 KB
/
Copy path_strtok.c
File metadata and controls
70 lines (60 loc) · 1.27 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
#include "_strtok.h"
/**
* _strtok - split a string and return an array of each word of the string
* @str: string to split
* @delim: a set of bytes that delimit the string
*
* Description: The last element of the array is NULL
*
* Return: an array of words.
* else return NULL on error or no word found.
*/
char **_strtok(char *str, const char *delim)
{
char **ret = NULL;
int num_tok = 0, c = 0;
if (str == NULL || delim == NULL)
{
return (NULL);
}
num_tok = counttok(str, delim); /* to be implemented */
ret = malloc((num_tok * sizeof(char *)) + 1);
for (c = 0; c < num_tok + 1; c++)
{
if (c == 0)
ret[c] = strtok(str, delim);
else
ret[c] = strtok(NULL, delim);
if (ret[c] == NULL)
break;
}
return (ret);
}
/**
* counttok - count the number of tokens in a string
* @str: string to parse
* @delim: a set of bytes that delimit the string
*
* Return: the number of tokens or -1 on failure
*/
int counttok(const char *str, const char *delim)
{
int toks = 0, c = 0;
if (str == NULL || delim == NULL)
{
return (-1);
}
for (c = 0; str[c] != '\0';)
{
if (index(delim, str[c]))
{/* found delimiter */
c++;
continue;
}
/* Found token */
for (; str[c] != '\0' && !(index(delim, str[c])); c++)
;
toks++;
}
return (toks);
}