-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_nb.c
64 lines (57 loc) · 1.54 KB
/
ft_printf_nb.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_nb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vess <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 18:09:37 by jcampagn #+# #+# */
/* Updated: 2021/12/10 11:56:47 by vess ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_nbrlen(int nbr)
{
int i;
i = 0;
if (nbr <= 0)
i++;
while (nbr)
{
i++;
nbr = nbr / 10;
}
return (i);
}
char *ft_itoa(int nb)
{
char *str;
long int nbr;
int i;
i = 0;
str = 0;
nbr = nb;
str = (char *)malloc(sizeof(char) * ft_nbrlen(nb) + 1);
if (!str)
return (NULL);
if (nbr < 0)
str[0] = '-';
if (nbr < 0)
nbr = -nbr;
str[ft_nbrlen(nb) - i++] = '\0';
while (nbr >= 10)
{
str[ft_nbrlen(nb) - i++] = (nbr % 10) + 48;
nbr /= 10;
}
str[ft_nbrlen(nb) - i++] = (nbr % 10) + 48;
return (str);
}
int ft_printnumber(int nb)
{
char *str;
str = ft_itoa(nb);
ft_putstr(str);
free(str);
return (ft_nbrlen(nb));
}