-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putnbr.c
74 lines (67 loc) · 1.62 KB
/
ft_putnbr.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
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 08:23:14 by juhtoo-h #+# #+# */
/* Updated: 2024/09/04 16:31:55 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int size_generator(long n)
{
int size;
size = 0;
if (n == 0)
return (1);
if (n < 0)
{
n = -n;
size++;
}
while (n > 0)
{
n = n / 10;
size++;
}
return (size);
}
static char *ft_itoa(int n)
{
int i;
int size;
long num;
char *str;
num = (long)n;
size = size_generator(num);
str = (char *)malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
str[0] = '0';
if (num < 0)
{
str[0] = '-';
num = -num;
}
i = 0;
while (num > 0)
{
str[size - i - 1] = (num % 10) + '0';
num = num / 10;
i++;
}
str[size] = '\0';
return (str);
}
int ft_putnbr(int num)
{
int printf_len;
char *str;
printf_len = 0;
str = ft_itoa(num);
printf_len += ft_putstr(str);
free(str);
return (printf_len);
}