-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
68 lines (61 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tcakir-y <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 16:51:41 by tutku #+# #+# */
/* Updated: 2024/10/15 10:25:47 by tcakir-y ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int calc_num_len(long n)
{
int len;
len = 0;
if (n <= 0)
{
len++;
n *= -1;
}
while (n != 0)
{
len++;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *result;
int len;
long num;
num = n;
len = calc_num_len(num);
result = malloc((len + 1) * sizeof(char));
if (!result)
return (0);
result[len--] = '\0';
if (num < 0)
{
result[0] = '-';
num *= -1;
}
if (num == 0)
result[0] = '0';
while (num != 0)
{
result[len--] = (num % 10) + '0';
num /= 10;
}
return (result);
}
// int main(void)
// {
// int num;
// num = -12345;
// printf("digit amount %d\n", calc_num_len((long)num));
// printf("result: %s", ft_itoa(num));
// return (0);
// }