-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
67 lines (62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbopp <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 08:35:54 by lbopp #+# #+# */
/* Updated: 2016/11/14 11:19:42 by lbopp ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_fill(unsigned int div, int size, int i, int n)
{
unsigned int nb;
char *result;
result = NULL;
nb = n;
if (n < 0 && n != 0)
nb = -n;
if (!(result = (char*)malloc(sizeof(char) * (size + 1))))
return (0);
if (n < 0 && n != 0)
result[0] = '-';
if (n <= 0)
size--;
while (size > 0)
{
result[i] = nb / div + '0';
nb %= div;
div /= 10;
size--;
i++;
}
result[i] = '\0';
return (result);
}
char *ft_itoa(int n)
{
unsigned int div;
int size;
unsigned int nb;
int i;
size = 1;
i = 0;
nb = n;
if (n < 0 && n != 0)
{
nb = -n;
size++;
i++;
}
if (n == 0)
size++;
div = 1;
while (nb / 10 >= div)
{
div *= 10;
size++;
}
return (ft_fill(div, size, i, n));
}