-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrl_functions.c
121 lines (113 loc) · 2.01 KB
/
strl_functions.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
#include "main.h"
/**
* _strdup - duplicates a string in heap memory.
* @s: string pointer
* Return: string
*/
char *_strdup(const char *s)
{
char *new;
size_t len;
len = _strlen(s);
new = malloc(sizeof(char) * (len + 1));
if (new == NULL)
return (NULL);
_memcpy(new, s, len + 1);
return (new);
}
/**
* _strlen - Returns the lenght of a string.
* @s: string pointer
* Return: 0
*/
int _strlen(const char *s)
{
int len;
for (len = 0; s[len] != 0; len++)
{
}
return (len);
}
/**
* cmp_chars - compares the chars of strings
* @str: string
* @delim: delimiter
* Return: 1, 0 if not.
*/
int cmp_chars(char str[], const char *delim)
{
unsigned int i, j, k;
for (i = 0, k = 0; str[i]; i++)
{
for (j = 0; delim[j]; j++)
{
if (str[i] == delim[j])
{
k++;
break;
}
}
}
if (i == k)
return (1);
return (0);
}
/**
* _strtok - splits a string into tokens
* @str: string
* @delim: delimiter
* Return: the next token or NULL
*/
char *_strtok(char str[], const char *delim)
{
static char *splitted, *str_end;
char *str_start;
unsigned int i, bool;
if (str != NULL)
{
if (cmp_chars(str, delim))
return (NULL);
splitted = str;
i = _strlen(str);
str_end = &str[i];
}
str_start = splitted;/*Save the starting point of the token*/
if (str_start == str_end)
return (NULL);/*NULL if there are no more tokens*/
for (bool = 0; *splitted; splitted++)
{
if (splitted != str_start)
if (*splitted && *(splitted - 1) == '\0')
break;
for (i = 0; delim[i]; i++)
{
if (*splitted == delim[i])
{
*splitted = '\0';
if (splitted == str_start)
str_start++;
break;
}
}
if (bool == 0 && *splitted)
bool = 1;/*Set the flag(bool) if token has non-delimiter characters*/
}
if (bool == 0)
return (NULL);
return (str_start);
}
/**
* _isdigit - defines if string is a number
* @s: string
* Return: 1, 0 if otherwise
*/
int _isdigit(const char *s)
{
unsigned int i;
for (i = 0; s[i]; i++)
{
if (s[i] < 48 || s[i] > 57)
return (0);
}
return (1);
}