-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux_strings.c
138 lines (119 loc) · 2.52 KB
/
aux_strings.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
128
129
130
131
132
133
134
135
136
137
138
#include "shell.h"
/**
* _strlen - Measure the length of a string
*
* @str: The string whose length is needed
*
* Return: The length of the string (number of elements)
*/
int _strlen(char *str)
{
int i;
for (i = 0; *(str + i); i++)
;
return (i);
}
/**
* _strncmp - compare two strings
*
* @s1: string 1
* @s2: string 2
* @n: bytes to compare
*
* Return: 1 if two strings are iquals, 0 if don
**/
int _strncmp(const char *s1, const char *s2, int n)
{
char str1, str2;
while (n-- > 0)
{
str1 = *s1++;
str2 = *s2++;
if (str1 != str2)
return (0);
else if (str1 == '\0' || str2 == '\0')
return (0);
}
return (1);
}
/**
* _strtok - Splits a string in separated words
*
* @str: The string to be separated
* @delimiter: The delimiter to make the tokens
*
* Return: An array of pointers with each word
*/
char **_strtok(char *str, char delimiter)
{
char **words = NULL;
int count = 0, i;
if (str == NULL || *(str) == '\0' ||
(*(str) == delimiter && *(str + 1) == 0))
return (NULL);
for (i = 0; *(str + i); i++)
{
if ((*(str + i + 1) != 0 && *(str + i) == delimiter &&
*(str + i + 1) != delimiter) || (i == 0 && *(str + i) != delimiter))
count++;
}
if (count == 0)
return (NULL);
words = (char **)_calloc((count + 1), sizeof(char *));
if (words == NULL)
return (NULL);
count = 0;
i = 0;
if (*(str + i) != delimiter)
{
*(words + count) = write_word(str + i, delimiter);
count++;
i++;
}
for (; *(str + i); i++)
{
if (*(str + i + 1) != 0 && *(str + i) == delimiter &&
*(str + i + 1) != delimiter)
{
*(words + count) = write_word(str + i + 1, delimiter);
count++;
}
}
*(words + count) = NULL;
return (words);
}
/**
* write_word - Write a word in the desired direction
*
* @str: The string having the word
* @delimiter: The delimiter of the words
*
* Return: A pointer with a copy of str until first delimiter found
*/
char *write_word(char *str, char delimiter)
{
char *word;
int i = 0;
word = (char *)_calloc((length_d(str + i, delimiter) + 1), sizeof(char));
if (word == NULL)
return (NULL);
for (i = 0; *(str + i) != delimiter && *(str + i); i++)
*(word + i) = *(str + i);
*(word + i) = 0;
return (word);
}
/**
* length_d - Computes the length of a string until a delimiter
*
* @str: The string having the word beggining at firs char
* @delimiter: The delimiter to compute length
*
* Return: The length of the string
*/
int length_d(char *str, char delimiter)
{
int i = 0;
for (i = 0; *(str + i) != delimiter && *(str + i); i++)
;
return (i);
}