-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_int.c
66 lines (59 loc) · 1.54 KB
/
print_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
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 18:15:32 by yorlians #+# #+# */
/* Updated: 2023/02/25 16:04:04 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int get_len_for_itoa(int n)
{
int length;
length = 0;
if (n <= 0)
length = 1;
while (n != 0)
{
n = n / 10;
length++;
}
return (length);
}
char *itoa(long n)
{
int length;
char *str;
length = get_len_for_itoa(n);
str = (char *)malloc(sizeof(char) * length + 1);
if (!str)
return (str);
if (n < 0)
{
str[0] = '-';
n = -n;
}
if (n == 0)
str[0] = '0';
str[length--] = '\0';
while (n != 0)
{
str[length] = (char)(n % 10) + '0';
length--;
n = n / 10;
}
return (str);
}
int print_int(int n)
{
int length;
char *number;
length = 0;
number = itoa(n);
length = print_string(number);
free(number);
return (length);
}