-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_trim_2darray.c
53 lines (49 loc) · 888 Bytes
/
_trim_2darray.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
#include "shell.h"
/**
* _count_non_empty_string - function that counts
* number of non empty string inside of 2d array
*
* @arr: 2d array to lookup for number of non empty
* string
* Return: number of non empty strings
*/
int _count_non_empty_string(char **arr)
{
int len;
len = 0;
while (*arr)
{
len += (**arr != '\0');
arr++;
}
return (len);
}
/**
* _trim_2darray - function takes an array
* and return new one with no empty string
*
* @arr: 2d array to remove non empty strings
* from it
* Return: 2d array without empty strings
*/
char **_trim_2darray(char **arr)
{
int len;
char **new_arr;
len = _count_non_empty_string(arr);
new_arr = malloc(sizeof(char *) * (len + 1));
if (!new_arr)
return (NULL);
new_arr[len] = NULL;
len = 0;
while (*arr)
{
if (**arr)
{
new_arr[len] = _strdup(*arr);
len++;
}
arr++;
}
return (new_arr);
}