-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_helper.c
91 lines (74 loc) · 1.26 KB
/
string_helper.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
#include "shell.h"
/**
* _strchr - function like strchr: read the manual
* @str: string
* @c: integer
* Return: NULL or true
*/
char *_strchr(const char *str, int c)
{
while (*str != '\0' && *str != (char)c)
{
str++;
}
return ((*str == (char)c) ? (char *)str : NULL);
}
/**
* _strdup - function like strdup.
* @str: String.
* Return: new String.
*/
char *_strdup(char *str)
{
char *new_str;
int len = _strlen(str);
new_str = malloc(sizeof(char) * (len + 1));
if (new_str == NULL)
{
perror("malloc failed");
exit(EXIT_FAILURE);
}
_strcpy(new_str, str);
return (new_str);
}
/**
* _strcmp - function like strcmp
* @s1: string
* @s2: string
* Return: s1 - s2
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 != '\0' && *s2 != '\0')
{
if (*s1 != *s2)
return (*s1 - *s2);
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* _strtok - function like strtok.
* @str: string
* @delim: delims
* Return: Nothing.
*/
char *_strtok(char *str, const char *delim)
{
static char *pos;
char *start, *end;
if (str)
pos = str;
if (!pos)
return (NULL);
while (*pos && strchr(delim, *pos))
pos++;
if (!*pos)
return (NULL);
start = pos;
end = start + strcspn(pos, delim);
pos = (*end) ? end + 1 : NULL;
*end = '\0';
return (start);
}