-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
68 lines (62 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
62
63
64
65
66
67
68
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/10/07 14:00:49 by jvan-hal #+# #+# */
/* Updated: 2022/11/10 12:23:53 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int getlength(int n)
{
int count;
count = 0;
if (n < 0)
{
++count;
n *= -1;
}
while ((n / 10) > 0)
{
++count;
n /= 10;
}
if (n < 10)
++count;
return (count);
}
static char *getstr(long int n, int len)
{
char *c;
c = malloc(len + 1);
if (!c)
return (NULL);
if (n < 0)
{
*c = '-';
n *= -1;
}
c += len;
*c = '\0';
--c;
while (n > 9)
{
*c = (n % 10) + '0';
n /= 10;
--c;
}
*c = n + '0';
if (*(c - 1) == '-')
return (c - 1);
return (c);
}
char *ft_itoa(int n)
{
if (n == -2147483648)
return (ft_strdup("-2147483648"));
return (getstr(n, getlength(n)));
}