-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
executable file
·117 lines (98 loc) · 1.57 KB
/
helpers.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "main.h"
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _puts - prints a string, followed by a new line, to stdout.
* @str: string to print
*/
void _puts(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
_putchar(str[i]);
i++;
}
}
/**
* _putint - prints an integer.
* @n: integer to print.
*
* Return: -1 on failure of dipr (malloc), 0 otherwise.
*/
int _putint(int n)
{
char *intStr = dipr(n);
if (!intStr)
return (-1);
_puts(intStr);
free(intStr);
return (0);
}
/**
* dipr - convert integer types to strings and returns pointer to string
* @n: integer to convert to string
*
* Return: pointer to new string
*/
char *dipr(int n)
{
int len, i, copy, input, end;
char *numStr;
input = n;
copy = input;
len = 1;
if (input == INT_MIN)
return ("-2147483648");
if (input < 0)
{
len++;
input *= -1;
}
while (copy / 10 != 0)
{
copy /= 10;
len++;
}
numStr = malloc(sizeof(*numStr) * (len + 1 + 1));
if (numStr == NULL)
return (NULL);
end = 0;
if (copy < 0)
{
numStr[0] = '-';
end = 1;
}
for (i = len - 1; i >= end; i--)
{
numStr[i] = input % 10 + '0';
input /= 10;
}
return (numStr);
}
/**
* print_array - prints an array of strings.
* @array: array to print
*/
void print_array(char **array)
{
int i = 0;
while (array[i] != NULL)
{
_puts(array[i]);
_putchar('\n');
i++;
}
if (array[i] == NULL)
_puts("NULL\n");
}