-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itou.c
72 lines (65 loc) · 1.61 KB
/
ft_itou.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itou.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccastill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/11 04:34:30 by ccastill #+# #+# */
/* Updated: 2020/07/11 04:38:05 by ccastill ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_count_nb2(unsigned long int nb)
{
int count;
unsigned int c;
c = 0;
count = 0;
if (nb < 0)
{
c = nb * -1;
count++;
}
if (nb > 0)
c = nb;
while (c >= 10)
{
c = c / 10;
count++;
}
if (c < 10)
count++;
return (count);
}
void ft_putnum2(char *s, unsigned long int n, int l)
{
unsigned int c;
c = 0;
s[l--] = '\0';
if (n == 0)
s[0] = '0';
if (n < 0)
{
s[0] = '-';
c = n * -1;
}
if (n > 0)
c = n;
while (c > 10 || (c <= 10 && c != 0))
{
s[l--] = c % 10 + 48;
c = c / 10;
}
}
char *ft_itou(unsigned long int n)
{
char *new;
int l;
l = ft_count_nb2(n);
new = (char*)malloc(sizeof(char) * l + 1);
if (new == 0)
return (NULL);
ft_putnum2(new, n, l);
return (new);
}