-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
59 lines (54 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ulyildiz <ulyildiz@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 08:40:15 by ulyildiz #+# #+# */
/* Updated: 2023/10/22 00:32:29 by ulyildiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_order(int n)
{
int i;
float j;
j = (float)n;
i = 0;
if (n == 0)
return (1);
while (j >= 1 || j <= -1)
{
j /= 10;
i++;
if (j > -1 && j < 0)
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *array;
int j;
j = ft_order(n);
if (n == -2147483648)
return (ft_strdup("-2147483648"));
array = (char *)malloc(j + 1);
if (array == NULL)
return (NULL);
array[j] = '\0';
if (n < 0)
{
array[0] = '-';
n *= -1;
}
if (n == 0)
array[0] = '0';
while (n > 0)
{
array[--j] = (n % 10) + '0';
n /= 10;
}
return (array);
}