-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_strtok.c
81 lines (76 loc) · 2 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
#include "shell.h"
char *_strchr(const char *s, int c);
size_t _strspn(const char *s1, const char *s2);
size_t _strcspn(const char *s1, const char *s2);
/**
* _strtok - breaks the string s1 into tokens and null-terminates them.
* Delimiter-Characters at the beginning and end
*of str are skipped. On each subsequent call delim may change.
* @str: string to tokenize
* @delim: string with the character that delimit srt.
* Return: the first/next token if possible, a null-pointer otherwise.
**/
char *_strtok(char *str, const char *delim)
{
static char *p;
if (str)
p = str;
else if (!p)
return (0);
str = p + _strspn(p, delim);
p = str + _strcspn(str, delim);
if (p == str)
return (p = 0);
p = *p ? *p = 0, p + 1 : 0;
return (str);
}
/**
* _strcspn - computes the length of the maximum initial segment of the string
* pointed to by s1which consists entirely of characters not from the
* string pointed to by s2.
* @s1: string to check
* @s2: string useful to compare
* Return: the length of the segment.
**/
size_t _strcspn(const char *s1, const char *s2)
{
size_t ret = 0;
while (*s1)
{
if (_strchr(s2, *s1))
return (ret);
s1++, ret++;
}
return (ret);
}
/**
* _strspn - computes the length of the maximum initial segment of the string
* pointed to by s1 which consists entirely of characters from the string
* pointed to by s2.
* @s1: strint to compute the lengh
* @s2: string delimit
* Return: the length of the segment.
**/
size_t _strspn(const char *s1, const char *s2)
{
size_t ret = 0;
while (*s1 && _strchr(s2, *s1++))
ret++;
return (ret);
}
/**
* _strchr - locates the first occurrence of c (converted to a char) in the
* string pointed to by s. The terminating null character is considered to be
* part of the string.
* @s: string
* @c: character
* Return: a pointer to the located character, or a null pointer
* if the character does not occur in the string.
**/
char *_strchr(const char *s, int c)
{
while (*s != (char)c)
if (!*s++)
return (0);
return ((char *)s);
}