-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
63 lines (57 loc) · 1.46 KB
/
ft_itoa.c
File metadata and controls
63 lines (57 loc) · 1.46 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchemin <tchemin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 14:13:34 by tchemin #+# #+# */
/* Updated: 2025/11/10 14:53:53 by tchemin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_length_of_int(int n)
{
int i;
i = 1;
if (n < 0)
i++;
if (n > 0)
n *= -1;
while (n < -9)
{
n = n / 10;
i++;
}
return (i);
}
int ft_get_buf(int n)
{
if (n < 0)
return (n * -1);
return (n);
}
char *ft_itoa(int n)
{
char *s;
int length;
int i;
int is_neg;
is_neg = 0;
i = 0;
length = ft_length_of_int(n);
s = ft_calloc(length + 1, sizeof(char));
if (!s)
return (NULL);
if (n < 0)
is_neg = 1;
while (i < length)
{
s[length - 1 - i++] = ft_get_buf((n % 10)) + 48;
n = n / 10;
}
if (is_neg)
s[0] = '-';
s[length] = '\0';
return (s);
}