-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
66 lines (61 loc) · 1.59 KB
/
ft_itoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rvandepu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/19 17:40:52 by rvandepu #+# #+# */
/* Updated: 2024/10/15 04:34:21 by rvandepu ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_itoa(int n)
{
char b[11];
char *s;
int i;
int j;
ft_bzero(b, 11);
if (n < 0)
b[0] = '-';
i = 10;
while (n != 0)
{
b[i--] = '0' + n % 10 * ((n > 0) - (n < 0));
n /= 10;
}
s = malloc(10 - i + 1 + (b[0] == '-') + (i == 10 && (b[10] = '0')));
if (s == NULL)
return (s);
i = 0;
j = 0;
while (i < 11)
if (b[i++])
s[j++] = b[i - 1];
s[j] = '\0';
return (s);
}
void ft_itoa_buf(int n, char b[12])
{
int i;
int j;
ft_bzero(b, 12);
if (n < 0)
b[0] = '-';
i = 10;
while (n != 0)
{
b[i--] = '0' + n % 10 * ((n > 0) - (n < 0));
n /= 10;
}
if (i == 10)
b[10] = '0';
i = 0;
j = 0;
while (i < 11)
if (b[i++])
b[j++] = b[i - 1];
b[j] = '\0';
}