-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
73 lines (65 loc) · 1.58 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
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emdi-mar <emdi-mar@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/08 21:56:13 by emdi-mar #+# #+# */
/* Updated: 2024/11/08 21:56:25 by emdi-mar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t check_sign(long int n)
{
size_t s;
s = 0;
if (n < 0)
s++;
return (s);
}
static size_t count_digits(long int n)
{
size_t num;
num = 0;
if (!n)
num = 1;
while (n)
{
n = n / 10;
num++;
}
return (num);
}
static void convert(long int n, char *p, size_t s, size_t len)
{
if (!n)
*p = 48;
while (n)
{
p[len - 2] = n % 10 + 48;
n = n / 10;
len--;
}
if (s)
*p = 45;
}
char *ft_itoa(int n)
{
char *p;
size_t len;
size_t s;
size_t digits;
long int ln;
p = NULL;
ln = n;
s = check_sign(ln);
if (s)
ln = ln * -1;
digits = count_digits(ln);
len = s + digits + 1;
p = ft_calloc(len, sizeof(*p));
if (p)
convert(ln, p, s, len);
return (p);
}