-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ltoa.c
72 lines (65 loc) · 1.69 KB
/
ft_ltoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rabougue <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/07/07 23:35:22 by rabougue #+# #+# */
/* Updated: 2016/07/08 01:29:51 by rabougue ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/libft.h"
static long checksign(long n)
{
if (n < 0)
return (1);
return (0);
}
static char *cpy(long stock, char *str)
{
char *cpystr;
if (stock == 1)
{
str[0] = '-';
cpystr = str + 1;
}
else
cpystr = str;
return (cpystr);
}
static void incr(long *i, long *res)
{
while (*res != 0)
{
*res = *res / 10;
*i = *i + 1;
}
}
char *ft_ltoa(long n)
{
long i;
long stock;
long res;
char *str;
char *cpystr;
i = 0;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
stock = checksign(n);
stock == 1 ? n = -n : (void)n;
res = n;
if (res == 0)
i = 1;
incr(&i, &res);
if (!(str = (char *)malloc(sizeof(char) * (i + stock + 1))))
return (NULL);
cpystr = cpy(stock, str);
cpystr[i] = '\0';
while (i--)
{
cpystr[i] = (n % 10) + '0';
n /= 10;
}
return (str);
}