-
Notifications
You must be signed in to change notification settings - Fork 1
/
strtoarr.c
65 lines (56 loc) · 1.26 KB
/
strtoarr.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
#include "shell.h"
/**
* strtoarr - converts a string to an array based on a delimiter
* @str: the string to be converted to an array
* @delim: the delimiter used to break string
*
* Return: array of the tokenized string, else NULl if not converted
*/
char **strtoarr(char *str, char delim)
{
int i = 0, j = 0, k = 0;
int str_len = _strlen(str);
char **arr = (char **)malloc(sizeof(char *) * (str_len + 1));
char *buf = (char *)malloc(sizeof(char) * (str_len + 1));
for (; i < str_len + 1; i++)
{
if (str[i] != delim && str[i] != '\0')
{
buf[k] = str[i];
k++;
}
else
{
if (k > 0)
{
buf[k] = '\0';
arr[j] = malloc(sizeof(char) * (k + 1));
_strcpy(arr[j], buf);
j++;
k = 0;
}
}
}
arr[j] = NULL;
free(buf);
return (arr);
}
/**
* _strncmp - compares 2 strings till n char
* @str1: first string to be compared with
* @str2: second string compared on/with first string
* @n: number of string to be compared
* Return: 0 if equal, positive if s1 < s2, negative if s1 > s2
*/
int _strncmp(const char *str1, const char *str2, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
if (str1[i] == '\0' || str2[i] == '\0')
return (str1[i] - str2[i]);
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
}
return (0);
}