-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
70 lines (64 loc) · 1.6 KB
/
ft_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhagon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 10:28:41 by juhagon #+# #+# */
/* Updated: 2021/11/11 09:59:44 by juhagon ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long count_digits(int n)
{
int i;
i = 0;
if (n == 0)
{
return (1);
}
while (n)
{
n = n / 10;
++i;
}
return (i);
}
static void set_neg(long *n)
{
if (n[0] < 0)
{
n[0] *= -1;
n[1] = 2;
}
else
n[1] = 1;
}
char *ft_itoa(int n)
{
char *rtn;
long x[2];
int digits;
x[0] = n;
set_neg(x), digits = count_digits(n);
rtn = malloc(sizeof(char) * digits + x[1]);
if (!rtn)
return (NULL);
if (x[1] == 2)
rtn[digits + 1] = '\0';
else
rtn[digits--] = '\0';
while (x[0] >= 10)
{
rtn[digits--] = (x[0] % 10) + '0';
x[0] = x[0] / 10;
}
if (x[0] < 10 && x[0] != 0)
rtn[digits] = x[0] + '0';
else if (x[0] == 0)
rtn[digits] = '0';
if (x[1] == 2)
rtn[0] = '-';
return (rtn);
}