-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils4.c
More file actions
131 lines (119 loc) · 2.52 KB
/
string_utils4.c
File metadata and controls
131 lines (119 loc) · 2.52 KB
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
#include "main.h"
/**
* _strlen - Returns the length of a string.
* @str: The input string.
*
* Return: The length of the string.
*/
int _strlen(const char *str)
{
int len;
for (len = 0; str[len] != 0; len++)
{
}
return (len);
}
/**
* _isdigit - Determines if a string consists only of numeric digits.
* @str: The input string to be checked.
*
* Return: 1 if the string consists only of numeric digits, 0 otherwise.
*/
int _isdigit(const char *str)
{
unsigned int i;
for (i = 0; str[i]; i++)
if (str[i] < 48 || str[i] > 57)
return (0);
return (1);
}
/**
* _strdup - Duplicates a string in the heap memory.
* @str: Type char pointer representing the string to duplicate.
*
* Return: Pointer to the duplicated string.
*/
char *_strdup(const char *str)
{
char *new;
size_t length;
length = _strlen(str);
new = malloc(sizeof(char) * (length + 1));
if (new == NULL)
return (NULL);
_memcpy(new, str, length + 1);
return (new);
}
/**
* compare_chars - Compares characters of strings.
* @str: Input string to compare.
* @delimiter: Delimiter string for comparison.
*
* Return: 1 if the characters are equal, 0 otherwise.
*/
int compare_chars(char str[], const char *delimiter)
{
unsigned int i, l, m;
for (i = 0, m = 0; str[i]; i++)
{
for (l = 0; delimiter[l]; l++)
{
if (str[i] == delimiter[l])
{
m++;
break;
}
}
}
if (i == m)
return (1);
return (0);
}
/**
* _strtok - Splits a string by a specified delimiter.
* @str: Input string to be split.
* @delimiter: Delimiter string.
*
* Return: Pointer to the next token in the string,
* or NULL if no more tokens are found.
*/
char *_strtok(char str[], const char *delimiter)
{
static char *splitted, *str_end;
char *str_start;
unsigned int i, bool;
if (str != NULL)
{
if (compare_chars(str, delimiter))
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; delimiter[i]; i++)
{
if (*splitted == delimiter[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);
}