-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
63 lines (57 loc) · 1.1 KB
/
array.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
/*
** EPITECH PROJECT, 2017
** lib
** File description:
** array
*/
#include "header_lib.h"
char *array_to_str(int argc, char **argv)
{
pos_t *stat = strlen_array(argc, argv);
int pos = 0;
char *rt = malloc(sizeof(stat->x + 1));
for (int i = 1; i < argc; i++) {
for (int k = 0;argv[i][k]; k++) {
rt[pos] = argv[i][k];
pos++;
}
if (pos < (stat->x - stat->y)) {
rt[pos] = ' ';
pos++;
}
}
rt[pos+1] = '\0';
return (rt);
}
pos_t *strlen_array(int argc, char **argv)
{
pos_t *rt;
rt = malloc(sizeof(pos_t));
rt->x = 0;
rt->y = 0;
for (int i = 1; i < argc;i++, rt->x++)
for (rt->y = 0; argv[i][rt->y]; rt->y++, rt->x++);
return (rt);
}
char **str_to_array(fs_t *sta)
{
int compt = 0;
int u = 0;
int size = word_counter(sta->txt);
char **tab = malloc(sizeof(char *) * (size + 1));
tab[compt] = malloc(sizeof(char) * (strle_nw(sta) + 1));
for (int i = 0;sta->txt[i]; i++)
if (sta->txt[i] == ' ')
if (sta->txt[i-1] == ' ')
i++;
else {
compt++;
tab[compt] = malloc((strle_nw(sta)- 1));
u = 0;
}
else {
tab[compt][u] = sta->txt[i];
u++;
}
return (tab);
}