-
Notifications
You must be signed in to change notification settings - Fork 0
/
_strtok.c
113 lines (97 loc) · 1.77 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "shell.h"
/**
* _finddel - finds the delimiter where it matches in the string
* @str: string input to check
* @delimit: delimiters to check for
* Return: matched delimiter
*/
char _finddel(char *str, const char *delimit)
{
int i = 0;
int j;
while (str[i])
{
j = 0;
while (delimit[j])
{
if (str[i] == delimit[j])
return (delimit[j]);
j++;
}
i++;
}
return (delimit[0]);
}
/**
* _strchr - located the delimiter in the string
* @str: string to check
* @c: delimiter to look for
* Return: returns the string after the delimiter, otherwise NULL
*/
char *_strchr(char *str, char c)
{
while (*str)
{
if (*str == c)
return (str);
str++;
}
if (c == 0)
return (str);
return (NULL);
}
/**
* _strtok2 - homemade strtok function, breaks up a input string into tokens
* @str_in: input string to tokenize
* @delimit: delimiters used to make tokens from string
* Return: pointer to the current delimited token
*/
char *_strtok2(char *str_in, const char *delimit)
{
static char *string;
char *token = NULL;
char *next = NULL;
int i = 0;
char deli = '\0';
if (str_in)
string = str_in;
token = string;
if (token == NULL)
return (NULL);
deli = _finddel(string, delimit);
next = _strchr(string, deli);
if (next)
{
i = _strlen(string);
i -= _strlen(next);
token = string;
next++;
string = next;
token[i] = '\0';
}
if (next == NULL)
{
token = string;
string = NULL;
}
return (token);
}
/**
* _strtok - main strtok function
* @str: string to tokenize
* @delim: delimiters
* Return: tokens from string
*/
char *_strtok(char *str, const char *delim)
{
char *token;
token = _strtok2(str, delim);
while (token)
{
if (token[0] == '\0')
token = _strtok2(NULL, delim);
else
return (token);
}
return (NULL);
}