-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
61 lines (56 loc) · 1.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myoshie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/08 05:57:53 by myoshie #+# #+# */
/* Updated: 2022/05/28 12:47:53 by myoshie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_len(long n)
{
size_t len;
len = 0;
if (n == 0)
return (1);
if (n < 0)
{
len++;
n *= -1;
}
while (n)
{
len++;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *s;
size_t s_len;
long ln;
ln = (long)n;
s_len = get_len(ln);
s = (char *)malloc(sizeof(char) * (s_len + 1));
if (!s)
return (NULL);
s[s_len] = '\0';
if (ln == 0)
s[0] = '0';
if (ln < 0)
{
s[0] = '-';
ln *= -1;
}
while (ln)
{
s[s_len - 1] = ln % 10 + '0';
ln /= 10;
s_len--;
}
return (s);
}