-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
99 lines (85 loc) · 2.17 KB
/
ft_itoa.c
File metadata and controls
99 lines (85 loc) · 2.17 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
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sfabi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/12 14:45:35 by sfabi #+# #+# */
/* Updated: 2023/10/12 14:45:37 by sfabi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int countlen(int n);
static char *caso_0(void);
char *ft_itoa(int n)
{
int indice;
int end;
char *result;
unsigned int num;
if (n == 0)
return (caso_0());
indice = countlen(n);
end = 0;
result = malloc(sizeof(char) * (indice + 1));
if (result == NULL)
return (NULL);
result[indice] = '\0';
num = n;
if (n < 0)
{
result[end++] = '-';
num = -n;
}
while (end < indice--)
{
result[indice] = '0' + num % 10;
num = num / 10;
}
return (result);
}
static int countlen(int n)
{
unsigned int num;
long int cont;
int len;
cont = 1;
len = 0;
if (n < 0)
{
len++;
num = n * -1;
}
else
num = n;
while (num / cont > 0)
{
cont = cont * 10;
len++;
}
return (len);
}
static char *caso_0(void)
{
char *result;
result = malloc(sizeof(char) * 2);
if (result == NULL)
return (NULL);
result[0] = '0';
result[1] = '\0';
return (result);
}
/*
int main() {
int num = -304293468; // Sostituisci con il numero che desideri convertire
char *str = ft_itoa(num);
if (str == NULL) {
printf("Errore nell'allocazione di memoria.\n");
return 1;
}
printf("Numero intero: %d\n", num);
printf("Numero convertito in stringa: %s\n", str);
free(str); // Assicurati di liberare la memoria allocata
return 0;
}*/