-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtok.c
68 lines (63 loc) · 1.08 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
64
65
66
67
68
#include "main.h"
/**
* similar - checks if character matches any value in string
* @chr: character
* @string: string
*
* Return: 1 if match, 0 if not
*/
unsigned int similar(char chr, const char *string)
{
unsigned int i;
for (i = 0; string[i] != '\0'; i++)
{
if (chr == string[i])
return (1);
}
return (0);
}
/**
* _strtok - my version of the strtok function
* @str: string tokenized
* @delim: splitter
*
* Return: pointer to the next token or NULL
*/
char *_strtok(char *str, const char *delim)
{
static char *token;
static char *next;
unsigned int i;
if (str != NULL)
next = str;
token = next;
if (token == NULL)
return (NULL);
for (i = 0; next[i] != '\0'; i++)
{
if (similar(next[i], delim) == 0)
break;
}
if (next[i] == '\0' || next[i] == '#')
{
next = NULL;
return (NULL);
}
token = next + i;
next = token;
for (i = 0; next[i] != '\0'; i++)
{
if (similar(next[i], delim) == 1)
break;
}
if (next[i] == '\0')
next = NULL;
else
{
next[i] = '\0';
next = next + i + 1;
if (*next == '\0')
next = NULL;
}
return (token);
}