-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstring_helpers2.c
67 lines (59 loc) · 1.13 KB
/
string_helpers2.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
#include "holberton.h"
/**
* _strtok - tokenizes strings at delimiter
* @str: input string
* @delim: delimiter
* Return: pointer to start of string
*/
char *_strtok(char *str, char *delim)
{
static char *lastptr;
char ch;
if (str == NULL)
str = lastptr;
do {
ch = *str++;
if (!ch)
return (NULL);
} while (_strchr(delim, ch));
str--;
lastptr = str + _strcspn(str, delim);
if (*lastptr)
*lastptr++ = 0;
return (str);
}
/**
* _strcspn - returns first occurence of any char in
* second string in first string
* @string: input string to search
* @chars: input chars to check
* Return: pointer to first match
*/
int _strcspn(char *string, char *chars)
{
char c;
char *p, *s;
for (s = string, c = *s; c; s++, c = *s)
for (p = chars; *p; p++)
if (c == *p)
return (s - string);
return (s - string);
}
/**
* _strchr - locates a character in a string
* @s: string to be searched
* @c: target char
* Return: pointer to first occurrence of c or NULL if char not found
*/
char *_strchr(char *s, char c)
{
char x;
while (true)
{
x = *s++;
if (x == c)
return (s - 1);
if (!x)
return (NULL);
}
}