-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_unsigned.c
62 lines (55 loc) · 1.5 KB
/
ft_print_unsigned.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ygunay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/17 18:50:12 by ygunay #+# #+# */
/* Updated: 2022/08/18 15:37:18 by ygunay ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_len_unsigned(unsigned int nb)
{
int len;
len = 0;
while (nb != 0)
{
nb /= 10;
len++;
}
return (len);
}
static char *ft_utoa(unsigned int nb)
{
int len;
char *result;
len = ft_len_unsigned(nb);
result = malloc(sizeof(char) * len + 1);
if (!result)
return (NULL);
result[len] = '\0';
while (nb != 0)
{
result[len - 1] = nb % 10 + '0';
nb /= 10;
len--;
}
return (result);
}
int ft_print_unsigned(unsigned int nb)
{
int len;
char *n;
len = 0;
if (nb == 0)
len += write(1, "0", 1);
else
{
n = ft_utoa(nb);
len += ft_print_str(n);
free(n);
}
return (len);
}