-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
executable file
·47 lines (42 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bscamand <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 16:35:17 by bscamand #+# #+# */
/* Updated: 2021/01/11 14:32:59 by bscamand ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
size_t ft_intlen(long long nbr, int signlen)
{
int intlen;
intlen = 1;
while (nbr /= 10)
intlen++;
return (intlen + signlen);
}
char *ft_itoa(long long n)
{
char *str;
int sign;
int intlen;
long long nbr;
sign = (n < 0) ? 1 : 0;
nbr = (n < 0) ? -(long long)n : (long long)n;
intlen = ft_intlen(nbr, sign);
str = (char *)malloc(sizeof(char) * (intlen + 1));
if (!str)
return (NULL);
str[intlen--] = '\0';
while (intlen >= sign)
{
str[intlen--] = (nbr % 10) + '0';
nbr /= 10;
}
if (sign == 1)
str[0] = '-';
return (str);
}