-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
90 lines (81 loc) · 1.92 KB
/
Copy pathft_itoa.c
File metadata and controls
90 lines (81 loc) · 1.92 KB
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: davmoren <davmoren@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 06:55:52 by davmoren #+# #+# */
/* Updated: 2024/05/04 07:46:54 by davmoren ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_intlen(int n)
{
int len;
len = 0;
if (n == 0)
return (1);
if (n < 0)
{
len++;
}
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_initialize(int a, int len)
{
char *result;
int isnegative;
isnegative = 0;
if (a < 0)
isnegative = 1;
result = (char *)malloc(sizeof(char) * (len + 1));
if (result == NULL)
return (NULL);
result[len] = '\0';
if (a == 0)
{
result[0] = '0';
return (result);
}
if (a == 1)
{
result[0] = '1';
return (result);
}
if (isnegative)
result[0] = '-';
return (result);
}
void ft_fill(char *resultado, int a, int len)
{
int posicion;
int isnegative;
isnegative = 0;
if (a < 0)
isnegative = 1;
while (a)
{
posicion = a % 10;
if (isnegative)
posicion = -posicion;
resultado[--len] = '0' + posicion;
a /= 10;
}
}
char *ft_itoa(int a)
{
int lenn;
char *resultado;
lenn = ft_intlen(a);
resultado = ft_initialize(a, lenn);
if (resultado == NULL || resultado[0] == '0')
return (resultado);
ft_fill(resultado, a, lenn);
return (resultado);
}