-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vduriez <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/17 19:45:34 by vduriez #+# #+# */
/* Updated: 2020/09/03 11:13:05 by vduriez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <stdlib.h>
long ft_size(int n)
{
int size;
int signe;
signe = (n < 0) ? 1 : 0;
n = (n < 0) ? -n : n;
size = 1;
while (n > 9)
{
size++;
n /= 10;
}
return (signe + size);
}
char *ft_itoa(int n)
{
int size;
int signe;
char *res;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
signe = (n < 0) ? 1 : 0;
size = ft_size(n);
n = (n < 0) ? -n : n;
if (!(res = malloc(sizeof(char) * size + 1)))
return (NULL);
res[size] = '\0';
while (--size >= signe)
{
res[size] = (n % 10) + 48;
n /= 10;
}
if (signe)
res[0] = '-';
return (res);
}