-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
61 lines (56 loc) · 1.51 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: llatrice <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/25 14:15:38 by llatrice #+# #+# */
/* Updated: 2021/10/25 16:36:51 by llatrice ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t get_length(int n)
{
int del;
size_t dlina;
del = 10;
dlina = 0;
if (n == 0)
return (1);
if (n < 0)
dlina++;
while (n)
{
n = n / 10;
dlina++;
del = del * 10;
}
return (dlina);
}
char *ft_itoa(int n)
{
size_t i;
size_t len;
char *out;
i = 0;
len = get_length(n);
out = (char *)malloc(sizeof(char) * (len + 1));
if (!out)
return (NULL);
out[len--] = '\0';
if (n == (-1) * (2147483648))
out[len--] = '8';
if (n == (-1) * (2147483648))
n = n / 10;
if (n < 0)
out[i++] = '-';
if (n < 0)
n = n * (-1);
while (len + 1 > i)
{
out[len--] = (n % 10) + '0';
n = n / 10;
}
return (out);
}