-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_to_tab.c
67 lines (62 loc) · 1.38 KB
/
path_to_tab.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
/*
** EPITECH PROJECT, 2022
** str_to_tab.c
** File description:
** str_to_tab
*/
#include "my.h"
#include <unistd.h>
#include <stdlib.h>
int size_tab_path(char *str)
{
int count = 0;
int s_count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ':') {
count++;
s_count = 0;
}
if (str[i] == ':' && s_count == 0) {
s_count++;
count++;
}
}
count++;
return count;
}
int size_word_path(char *str, int id)
{
int count = 0;
for (int i = id; str[i] != ':' && str[i] != '\0'; i++) {
count++;
}
return count;
}
void path_to_char2(char *str, int id, char **tab, int index)
{
int count = 0;
tab[index] = malloc((size_word_path(str, id) + 1) * sizeof(char));
for (int i = id; str[i] != ':' && str[i] != '\0'; i++) {
tab[index][count] = str[i];
count++;
}
tab[index][count] = '\0';
}
char **path_to_tab(char *str)
{
char **tab = malloc((size_tab_path(str)) * sizeof(char *) + sizeof(NULL));
if (tab == NULL)
return NULL;
int index = 0;
for (int i = 0; str[i] != '\0';) {
if (str[i] != ':') {
path_to_char2(str, i, tab, index);
i += size_word_path(str, i);
index++;
} else
i++;
}
tab[index] = malloc(sizeof(char));
tab[index] = NULL;
return tab;
}