-
Notifications
You must be signed in to change notification settings - Fork 1
/
_itoa.c
47 lines (46 loc) · 808 Bytes
/
_itoa.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
#include "main.h"
/**
* _itoa - convert integer to string
* @x: integer to convert to string
* Return: converted string
*/
char *_itoa(int x)
{
int i = 0, neg = 0, buf = BUFF;
char *y = NULL;
y = _realloc(y, 0, buf);
if (x == 0)
{
y[0] = '0', y[1] = '\0';
return (y);
}
else if (x < 0)
{
neg = 1, x++, x = -x, i++;
y[0] = x % 10 + 48 + 1;
x = x / 10;
}
while (x > 0)
{
y = ((i >= buf) ? _realloc(y, buf, buf + BUFF) : y);
if (!y)
free(y), exit(98);
buf = (i >= buf) ? buf + BUFF : buf;
y[i] = x % 10 + 48;
x /= 10;
i++;
}
if (neg == 1)
{
y = ((i >= buf) ? _realloc(y, buf, buf + BUFF) : y);
if (!y)
free(y), exit(98);
buf = (i >= buf) ? buf + BUFF : buf;
y[i] = '-';
i++;
}
y = _realloc(y, buf, i + 1);
y[i] = '\0';
_rev_string(y);
return (y);
}