-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_strtok.c
65 lines (58 loc) · 911 Bytes
/
_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
64
65
#include "holberton.h"
/**
* _strtok - separates string by token/delim
* @str: string to tokenize
* @delim: separation token
*
* Description: Searches string for delimiter and replaces it with a null byte
*
* Return: A pointer to first substring.
*/
char *_strtok(char *str, const char *delim)
{
char *b;
static char *e;
int idx = 0;
int j;
if (str != NULL)
b = str;
else
b = e;
if (*b == '\0')
return (NULL);
b += _strspn(b, delim);
while (b[idx] != '\0')
{
j = 0;
while (delim[j] != '\0')
{
if (b[idx] == delim[j])
{
b[idx] = '\0';
e = &b[idx + 1];
return (b);
}
j++;
}
idx++;
}
if (b[idx] == '\0')
{
if (idx >= 0)
{
if (b[idx - 1] == ' ')
return (NULL);
else if (b[idx - 1] == '\n')
{
if (idx >= 1)
{
if (b[idx - 2] == ' ')
return (NULL);
}
}
}
e = &b[idx];
return (b);
}
return (NULL);
}