-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
55 lines (50 loc) · 1.43 KB
/
ft_itoa.c
File metadata and controls
55 lines (50 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbiasuz <lbiasuz@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/21 10:09:50 by lbiasuz #+# #+# */
/* Updated: 2022/04/23 09:43:23 by lbiasuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int int_size(int n)
{
unsigned int i;
i = 0;
if (n == 0)
return (2);
else if (n < 0)
i++;
while (n)
{
i++;
n = n / 10;
}
return (i + 1);
}
char *ft_itoa(int n)
{
char *dest;
int negative;
unsigned int size;
negative = n < 0;
size = int_size(n);
dest = malloc(sizeof(char) * (size));
if (!dest)
return (NULL);
dest[--size] = '\0';
if (!n)
dest[--size] = '0';
while (n)
{
size--;
dest[size] = ((-negative + !negative) * (n % 10) + 48);
n = n / 10;
}
if (negative)
dest[--size] = '-';
return (dest);
}