-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
73 lines (65 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 13:36:58 by juhtoo-h #+# #+# */
/* Updated: 2024/08/30 10:41:38 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int size_generator(long n)
{
int size;
size = 0;
if (n == 0)
return (1);
if (n < 0)
{
n = -n;
size++;
}
while (n > 0)
{
n = n / 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
int i;
int size;
long num;
char *str;
num = (long)n;
size = size_generator(num);
str = (char *)malloc(sizeof(char) * (size + 1));
str[0] = '0';
if (num < 0)
{
str[0] = '-';
num = -num;
}
i = 0;
while (num > 0)
{
str[size - i - 1] = (num % 10) + '0';
num = num / 10;
i++;
}
str[size] = '\0';
return (str);
}
// #include <stdio.h>
// int main(void)
// {
// int n = 999999;
// char *str;
// str = ft_itoa(0);
// printf("%d\n", size_generator(n));
// printf("%s\n", str);
// free(str);
// }