-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_strtok.c
68 lines (63 loc) · 1.25 KB
/
new_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 "shell.h"
/**
* check_match - checks if a character matches any in a string
* @c: character to check
* @str: string to check
*
* Return: 1 if match, 0 if not
*/
unsigned int check_match(char c, const char *str)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++)
{
if (c == str[i])
return (1);
}
return (0);
}
/**
* new_strtok - custom strtok
* @str: string to tokenize
* @delim: delimiter to tokenize against
*
* Return: pointer to the next token or NULL
*/
char *new_strtok(char *str, const char *delim)
{
static char *token_start;
static char *next_token;
unsigned int i;
if (str != NULL)
next_token = str;
token_start = next_token;
if (token_start == NULL)
return (NULL);
for (i = 0; next_token[i] != '\0'; i++)
{
if (check_match(next_token[i], delim) == 0)
break;
}
if (next_token[i] == '\0' || next_token[i] == '#')
{
next_token = NULL;
return (NULL);
}
token_start = next_token + i;
next_token = token_start;
for (i = 0; next_token[i] != '\0'; i++)
{
if (check_match(next_token[i], delim) == 1)
break;
}
if (next_token[i] == '\0')
next_token = NULL;
else
{
next_token[i] = '\0';
next_token = next_token + i + 1;
if (*next_token == '\0')
next_token = NULL;
}
return (token_start);
}