-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
71 lines (65 loc) · 1.6 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: javellis <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 15:40:41 by javellis #+# #+# */
/* Updated: 2022/10/12 15:40:47 by javellis ### ########.fr */
/* */
/* ************************************************************************** */
#include<stdlib.h>
#include "libft.h"
static int ft_len(int n)
{
int i;
i = 1;
if (n < 0)
{
n *= -1;
i++;
}
else if (n == 0)
i++;
while (n != 0)
{
n /= 10;
i++;
}
return (i);
}
static char *ft_itoa2(int nbr, int i, char *str)
{
if (nbr == -2147483648)
{
str[--i] = '8';
nbr = -214748364;
}
if (nbr < 0)
{
str[0] = '-';
nbr *= -1;
}
i--;
while (nbr > 0)
{
str[i--] = (nbr % 10) + 48;
nbr = nbr / 10;
}
return (str);
}
char *ft_itoa(int nbr)
{
int i;
char *str;
i = ft_len(nbr);
str = (char *)malloc(sizeof(char) * i);
str[--i] = '\0';
if (nbr == 0)
{
str[0] = '0';
return (str);
}
return (ft_itoa2(nbr, i, str));
}