-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr.c
73 lines (66 loc) · 1.61 KB
/
ft_putnbr.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
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alukyane <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/09/25 14:52:37 by alukyane #+# #+# */
/* Updated: 2017/09/25 14:52:39 by alukyane ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_length(long long int n)
{
int length;
length = 0;
if (n < 0)
{
n *= -1;
length++;
}
while (n != 0)
{
n /= 10;
length++;
}
return (length);
}
static void ft_printnbr(int *arr, int j)
{
char *str;
int f;
f = 0;
str = (char *)malloc(sizeof(char) * (j + 1));
while (j-- > 0)
{
str[f++] = arr[j] + '0';
}
str[f] = '\0';
write(1, str, ft_strlen(str));
free(str);
}
void ft_putnbr(long long int nb)
{
int j;
int *arr;
int f;
arr = (int *)malloc(sizeof(int) * ft_length(nb));
f = 1;
j = 0;
if (nb == 0)
write(1, "0", 1);
if (nb < 0)
{
f = -1;
write(1, "-", 1);
}
while (nb != 0)
{
arr[j] = (nb % 10) * f;
nb = nb / 10;
++j;
}
ft_printnbr(arr, j);
free(arr);
}