-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_unsigned.c
64 lines (57 loc) · 1.51 KB
/
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
63
64
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_unsigned.c :+: :+: */
/* +:+ */
/* By: dreijans <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/23 12:43:37 by dreijans #+# #+# */
/* Updated: 2022/12/05 13:56:52 by dreijans ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int how_much(unsigned int a)
{
int i;
i = 0;
if (a <= 0)
i = i + 1;
while (a != 0)
{
a = a / 10;
i++;
}
return (i);
}
static char *u_toa(unsigned int n)
{
char *str;
int index;
index = how_much(n);
str = ft_calloc(index + 1, sizeof (char));
if (str == NULL)
return (NULL);
index--;
if (n == 0)
str[0] = '0';
while (n != 0)
{
str[index] = (n % 10) + '0';
n = n / 10;
index--;
}
return (str);
}
int print_unsigned(unsigned int n)
{
int count;
char *nbr;
count = -1;
nbr = u_toa(n);
if (nbr != NULL)
{
count = write (1, nbr, ft_strlen(nbr));
free (nbr);
}
return (count);
}