-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_func3.c
127 lines (120 loc) · 2.64 KB
/
str_func3.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "s_shell.h"
/**
* _strchr - locates a character in a string
* @str: the string
* @c: the character
* Return: a pointer to the memory area str
*/
char *_strchr(char *str, char c)
{
do {
if (*str == c)
return (str);
} while (*str++ != '\0');
return (NULL);
}
/**
* _strtok - splits a string into words. Repeat delimiters are ignored
* @str: the string
* @delimiters: the delimiter string
* Return: a pointer to an array of strings, or NULL in failure
*/
char **_strtok(char *str, char *delimiters)
{
int i, j, k, m, numWords = 0;
char **result;
if (str == NULL || str[0] == '\0')
return (NULL);
if (!delimiters)
delimiters = " ";
for (i = 0; str[i] != '\0'; i++)
{
if (!is_delimiter(str[i], delimiters) &&
(is_delimiter(str[i + 1], delimiters) || !str[i + 1]))
numWords++;
}
if (numWords == 0)
return (NULL);
result = malloc((1 + numWords) * sizeof(char *));
if (!result)
return (NULL);
for (i = 0, j = 0; j < numWords; j++)
{
while (is_delimiter(str[i], delimiters))
i++;
k = 0;
while (!is_delimiter(str[i + k], delimiters) && str[i + k])
k++;
result[j] = malloc((k + 1) * sizeof(char));
if (!result[j])
{
for (k = 0; k < j; k++)
free(result[k]);
free(result);
return (NULL);
}
for (m = 0; m < k; m++)
result[j][m] = str[i++];
result[j][m] = '\0';
}
result[j] = NULL;
return (result);
}
/**
* _strtok2 - splits a string into words
* @str: the string
* @delimiter: the delimiter
* Return: a pointer to an array of strings, or NULL on failure
*/
char **_strtok2(char *str, char delimiter)
{
int i, j, k, m, numWords = 0;
char **result;
if (str == NULL || str[0] == '\0')
return (NULL);
for (i = 0; str[i] != '\0'; i++)
{
if ((str[i] != delimiter && str[i + 1] == delimiter) ||
(str[i] != delimiter && !str[i + 1]) || str[i + 1] == delimiter)
numWords++;
}
if (numWords == 0)
return (NULL);
result = malloc((1 + numWords) * sizeof(char *));
if (!result)
return (NULL);
for (i = 0, j = 0; j < numWords; j++)
{
while (str[i] == delimiter && str[i] != delimiter)
i++;
k = 0;
while (str[i + k] != delimiter && str[i + k] && str[i + k] != delimiter)
k++;
result[j] = malloc((k + 1) * sizeof(char));
if (!result[j])
{
for (k = 0; k < j; k++)
free(result[k]);
free(result);
return (NULL);
}
for (m = 0; m < k; m++)
result[j][m] = str[i++];
result[j][m] = '\0';
}
result[j] = NULL;
return (result);
}
/**
* is_delimiter - check if character is a delimeter
* @c: char to check
* @dlm: delimeter
* Return: 1 if true 0 on failure
*/
int is_delimiter(char c, char *dlm)
{
while (*dlm)
if (*dlm++ == c)
return (1);
return (0);
}