-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putunbr.c
62 lines (55 loc) · 1.53 KB
/
ft_putunbr.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_putunbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 09:37:28 by juhtoo-h #+# #+# */
/* Updated: 2024/09/04 16:32:12 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int generate_len(unsigned int num)
{
int i;
if (num == 0)
return (1);
i = 0;
while (num > 0)
{
num = num / 10;
i++;
}
return (i);
}
static char *ft_uitoa(unsigned int num)
{
int i;
int len;
char *str;
len = generate_len(num);
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = 0;
str[i] = '0';
str[len] = '\0';
while (num > 0)
{
str[len - i - 1] = (num % 10) + '0';
num = num / 10;
i++;
}
return (str);
}
int ft_putunbr(unsigned int num)
{
int printf_len;
char *str;
printf_len = 0;
str = ft_uitoa(num);
printf_len += ft_putstr(str);
free(str);
return (printf_len);
}