Zappy - Year end project
2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
src
server
src
utils
array_utils.c
Go to the documentation of this file.
1
/*
2
** EPITECH PROJECT, 2023
3
** my_zappy
4
** File description:
5
** array_utils.c
6
*/
7
8
#include <string.h>
9
#include <stdlib.h>
10
11
#include "
utils.h
"
12
13
size_t
get_array_len
(
char
**array)
14
{
15
size_t
len = 0;
16
17
for
(; array[len] != NULL; ++len);
18
return
(len);
19
}
20
21
static
int
count_string_character(
const
char
*str,
const
char
delim)
22
{
23
int
count = 0;
24
25
for
(
int
i = 0; str[i] !=
'\0'
; i++) {
26
if
(str[i] != delim) {
27
count++;
28
}
else
{
29
return
(count);
30
}
31
}
32
return
(count);
33
}
34
35
static
int
count_string_number(
const
char
*str,
const
char
delim)
36
{
37
int
count = 0;
38
39
for
(
int
i = 0; str[i] !=
'\0'
; i++) {
40
if
(str[i] == delim) {
41
count++;
42
}
43
}
44
count++;
45
return
(count);
46
}
47
48
static
int
jump_n(
const
int
n,
const
int
size,
const
char
*str,
49
const
char
delim)
50
{
51
int
new_n = size + n;
52
53
while
(str[new_n] ==
'\0'
|| str[new_n] == delim) {
54
if
(str[new_n] ==
'\0'
) {
55
return
(new_n);
56
}
57
new_n = new_n + 1;
58
}
59
return
(new_n);
60
}
61
62
char
**
str_to_word_array
(
const
char
*str,
const
char
delim)
63
{
64
int
count = count_string_number(str, delim);
65
char
**tab = malloc(
sizeof
(
char
*) * (count + 1));
66
int
n = 0;
67
int
line = 0;
68
int
size = 0;
69
70
if
(tab == NULL)
71
return
(NULL);
72
while
(str[n] !=
'\0'
&& line < count) {
73
size = count_string_character(str + n, delim);
74
tab[line] = malloc(
sizeof
(
char
) * (size + 1));
75
if
(tab[line] == NULL)
76
return
(NULL);
77
strncpy(tab[line], str + n, size);
78
tab[line][size] =
'\0'
;
79
line++;
80
n = jump_n(n, size, str, delim);
81
}
82
tab[line] = NULL;
83
return
(tab);
84
}
get_array_len
size_t get_array_len(char **array)
! GETTER !!
Definition
array_utils.c:13
str_to_word_array
char ** str_to_word_array(const char *str, const char delim)
Turn string to char **.
Definition
array_utils.c:62
utils.h