-
Notifications
You must be signed in to change notification settings - Fork 29
/
aux_str2.c
129 lines (118 loc) · 2.16 KB
/
aux_str2.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
#include "holberton.h"
/**
* _strdup - duplicates a str in the heap memory.
* @s: Type char pointer str
* Return: duplicated str
*/
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: Type char pointer
* Return: Always 0.
*/
int _strlen(const char *s)
{
int len;
for (len = 0; s[len] != 0; len++)
{
}
return (len);
}
/**
* cmp_chars - compare chars of strings
* @str: input string.
* @delim: delimiter.
*
* Return: 1 if are equals, 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 by some delimiter.
* @str: input string.
* @delim: delimiter.
*
* Return: string splited.
*/
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; /*Store first address*/
i = _strlen(str);
str_end = &str[i]; /*Store last address*/
}
str_start = splitted;
if (str_start == str_end) /*Reaching the end*/
return (NULL);
for (bool = 0; *splitted; splitted++)
{
/*Breaking loop finding the next token*/
if (splitted != str_start)
if (*splitted && *(splitted - 1) == '\0')
break;
/*Replacing delimiter for null char*/
for (i = 0; delim[i]; i++)
{
if (*splitted == delim[i])
{
*splitted = '\0';
if (splitted == str_start)
str_start++;
break;
}
}
if (bool == 0 && *splitted) /*Str != Delim*/
bool = 1;
}
if (bool == 0) /*Str == Delim*/
return (NULL);
return (str_start);
}
/**
* _isdigit - defines if string passed is a number
*
* @s: input string
* Return: 1 if string is a number. 0 in other case.
*/
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);
}