-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_stringTok.c
52 lines (48 loc) · 1.04 KB
/
_stringTok.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
#include "main.h"
/**
*_stringTok - function to execute.
*@string: string to execute
*@delim: delimiter
*Return: 0 on success
*/
char **_stringTok(char *string, char *delim)
{
char **array_Of_Words = NULL, *token, *temp_ptr = NULL;
int counter = 0;
if (!string)
return (NULL);
temp_ptr = _strdup(string);
if (temp_ptr == NULL)
{
free(string), string = NULL;
perror("malloc");
return (NULL); }
token = strtok(temp_ptr, delim);
if (token == NULL) /*Handle spaces only*/
{
free(temp_ptr), temp_ptr = NULL;
free(string), string = NULL;
return (NULL); }
while (token)
{
counter++;
token = strtok(NULL, delim); }
free(temp_ptr), temp_ptr = NULL;
array_Of_Words = malloc((counter + 1) * (sizeof(char *)));
if (array_Of_Words == NULL)
{
free(string), string = NULL;
return (NULL);
}
counter = 0;
token = strtok(string, delim);
while (token)
{
array_Of_Words[counter] = _strdup(token);
token = strtok(NULL, delim);
counter++;
}
free(string), string = NULL;
array_Of_Words[counter] = NULL;
return (array_Of_Words);
}