-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
71 lines (65 loc) · 1.66 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
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atweek <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/11 19:20:42 by atweek #+# #+# */
/* Updated: 2020/11/12 19:12:39 by atweek ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intlen(int num)
{
int len;
int i;
if (num == 0)
return (1);
i = num;
len = 0;
if (num < 0)
{
len++;
num *= -1;
}
while (i != 0)
{
i /= 10;
len++;
}
return (len);
}
static char *max_or_null(int n, char *ch)
{
if (n == 0)
ft_strlcpy(ch, "0", 2);
if (n == INT32_MIN)
ft_strlcpy(ch, "-2147483648", 12);
return (ch);
}
char *ft_itoa(int num)
{
int len;
char *ch;
int n;
n = num;
len = ft_intlen(n);
ch = malloc(sizeof(char) * (len + 1));
if (ch)
{
if ((num == 0) || (num == INT32_MIN))
return (max_or_null(n, ch));
n = (n < 0) ? (n * -1) : n;
ch[len--] = '\0';
while (len != 0)
{
ch[len--] = (n % 10) + '0';
n = n / 10;
}
ch[len] = (num < 0) ? '-' : (n % 10) + '0';
return (ch);
}
else
return (NULL);
}