-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_unsigned_int.c
64 lines (57 loc) · 1.57 KB
/
print_unsigned_int.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_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 15:59:03 by yorlians #+# #+# */
/* Updated: 2023/02/25 17:21:49 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int get_len_for_uitoa(unsigned int n)
{
int length;
length = 0;
if (n <= 0)
length++;
while (n)
{
n = n / 10;
length++;
}
return (length);
}
char *uitoa(unsigned int n)
{
int length;
char *str;
length = get_len_for_uitoa(n);
str = (char *)malloc(sizeof(char) * length + 1);
if (!str)
return (str);
str[length] = '\0';
while (length != 0)
{
str[length - 1] = (n % 10) + '0';
n = n / 10;
length--;
}
return (str);
}
int print_unsigned_int(unsigned int n)
{
unsigned int length;
char *number;
length = 0;
if (n == 0)
return (write(1, "0", 1));
else
{
number = uitoa(n);
length = print_string(number);
free(number);
}
return (length);
}