-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ltoa.c
57 lines (53 loc) · 1.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: plisieck <plisieck@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/02/08 18:46:16 by plisieck #+# #+# */
/* Updated: 2015/02/08 18:46:17 by plisieck ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void lengths(long int n, size_t *len, long int *weight)
{
*len = 1;
if (n >= 0)
{
*len = 0;
n = -n;
}
*weight = 1;
while (n / *weight < -9)
{
*weight *= 10;
*len += 1;
}
}
char *ft_ltoa(long int n)
{
size_t len;
long int weight;
size_t cur;
char *str;
lengths(n, &len, &weight);
str = (char *)malloc(sizeof(*str) * (len + 1));
if (str == NULL)
return (NULL);
cur = 0;
if (n < 0)
{
str[cur] = '-';
cur++;
}
if (n > 0)
n = -n;
while (weight >= 1)
{
str[cur++] = -(n / weight % 10) + 48;
weight /= 10;
}
str[cur] = '\0';
return (str);
}