-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfree_the_array.c
More file actions
94 lines (82 loc) · 1.58 KB
/
free_the_array.c
File metadata and controls
94 lines (82 loc) · 1.58 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
#include "shell.h"
/**
* free_the_array - frees the array of pointers arv
*@array: array of pointers
*/
void free_the_array(char **array)
{
int i = 0;
if (!array)
{
return;
}
for (i = 0; array[i] != NULL; i++)
{
free(array[i]), array[i] = NULL;
}
free(array), array = NULL;
}
/**
* _intger_to_assci - converts a string to an integer
* @num: the string to be converted
* Return: 0 if no numbers in string, converted number otherwise
* -1 on error
*/
char *_intger_to_assci(int num)
{
char bf[20];
int i = 0;
if (num == 0)
{
bf[i++] = '0';
}
else
{
while (num > 0)
{
bf[i++] = (num % 10) + '0';
num /= 10;
}
}
bf[i] = '\0';
string_reverse(bf, i);
return (strdup(bf));
}
/**
* string_reverse - duplicates a string
* @st: the string to duplicate
* @length: size of string
* Return: pointer to the duplicated string
*/
void string_reverse(char *st, int length)
{
char temp;
int start = 0;
int ed = length - 1;
while (start < ed)
{
temp = st[start];
st[start] = st[ed];
st[ed] = temp;
start++;
ed--;
}
}
/**
* _printerror - prints an error message
* @cmd: the parameter & return info struct
* @nm: string containing specified error type
* @ix: an integer
*/
void _printerror(char *nm, char *cmd, int ix)
{
char *idx, massege[] = ": not found\n";
idx = _intger_to_assci(ix);
write(STDERR_FILENO, nm, string_len(nm));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, idx, string_len(idx));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmd, string_len(cmd));
write(STDERR_FILENO, massege, string_len(massege));
free(idx);
}