-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strtok.c
More file actions
44 lines (40 loc) · 739 Bytes
/
_strtok.c
File metadata and controls
44 lines (40 loc) · 739 Bytes
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
#include "main.h"
/**
* _strtok - tokenizing function
* @str: string to be tokenized.
* @delim: delimeter between strings
* Return: pointer to tokenized string. *
*/
char *_strtok(char *str, const char *delim)
{
static char *buf;
char *token, *tokenized_str;
int i = 0, j = 0;
if (str != NULL)
{
buf = str;
}
while (1)
{
while (*buf != '\0' && strchr(delim, *buf) != NULL)
buf++;
if (*buf == '\0')
return (NULL);
token = buf;
while (*buf != '\0' && strchr(delim, *buf) == NULL)
{
i++;
buf++;
}
tokenized_str = malloc(i + 1);
if (tokenized_str == NULL)
return (NULL);
while (j < i)
{
tokenized_str[j] = token[j];
j++;
}
tokenized_str[j] = '\0';
return (tokenized_str);
}
}