-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lltoa.c
52 lines (47 loc) · 1.39 KB
/
ft_lltoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jraymond <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/02 15:24:42 by jraymond #+# #+# */
/* Updated: 2018/02/14 16:47:53 by jraymond ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_countint(long long n)
{
int len;
if (n == 0)
return (1);
len = 0;
n < 0 ? len++ : 0;
while (n != 0)
{
n /= 10;
len++;
}
return (len++);
}
char *ft_lltoa(long long n)
{
int len;
char *buf;
int sign;
len = ft_countint(n);
buf = (char*)malloc(sizeof(char) * (len + 1));
if (!buf)
return (NULL);
buf[len] = '\0';
len--;
sign = (n < 0) ? 1 : 0;
while (len >= 0)
{
buf[len] = ft_llabs(n % 10) + '0';
len--;
n /= 10;
}
sign == 1 ? buf[0] = '-' : 0;
return (buf);
}