-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_strtok.c
63 lines (61 loc) · 1.01 KB
/
_strtok.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
#include "main.h"
/**
* check_delim - Checks If A Character Match Any Char *
* @c: Character To Check
* @str: String To Check
* Return: 1 Succes, 0 Failed
*/
unsigned int check_delim(char c, const char *str)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++)
{
if (c == str[i])
return (1);
}
return (0);
}
/**
* _strtok - Token A String Into Token (strtrok)
* @str: String
* @delim: Delimiter
* Return: Pointer To The Next Token Or NULL
*/
char *_strtok(char *str, const char *delim)
{
static char *ts;
static char *nt;
unsigned int i;
if (str != NULL)
nt = str;
ts = nt;
if (ts == NULL)
return (NULL);
for (i = 0; ts[i] != '\0'; i++)
{
if (check_delim(ts[i], delim) == 0)
break;
}
if (nt[i] == '\0' || nt[i] == '#')
{
nt = NULL;
return (NULL);
}
ts = nt + i;
nt = ts;
for (i = 0; nt[i] != '\0'; i++)
{
if (check_delim(nt[i], delim) == 1)
break;
}
if (nt[i] == '\0')
nt = NULL;
else
{
nt[i] = '\0';
nt = nt + i + 1;
if (*nt == '\0')
nt = NULL;
}
return (ts);
}