-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
90 lines (83 loc) · 1.75 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ochurko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 09:22:00 by ochurko #+# #+# */
/* Updated: 2023/11/22 17:52:01 by ochurko ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len(int n)
{
int len;
len = 0;
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
static char *ft_result(int n, int len, int min, char c)
{
char *str;
int i;
int j;
j = 0;
i = len - 1;
str = (char *) malloc (sizeof(char) * (len + 1 + min));
if (!str)
return (NULL);
str[len] = '\0';
if (c == '-')
{
str[0] = '-';
j = 1 + min;
if (min == 1)
str[1] = '2';
}
while (len > j)
{
len--;
i = n % 10;
n = n / 10;
str[len] = i + 48;
}
return (str);
}
char *ft_itoa(int n)
{
int len;
char neg;
char *str;
int min;
len = 0;
min = 0;
neg = '\0';
if (n == -2147483648)
{
min ++;
n = -147483648;
len ++;
}
if (n == 0)
len = 1;
if (n < 0)
{
n = -n;
neg = '-';
len++;
}
len = len + ft_len(n);
str = ft_result(n, len, min, neg);
return (str);
}
/*
int main()
{
printf("%s", ft_itoa(-2147483));
}
*/