-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
69 lines (62 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vangirov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/10 16:49:18 by vangirov #+# #+# */
/* Updated: 2021/12/10 22:34:18 by vangirov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intlen(long num)
{
int len;
if (num == 0)
return (1);
if (num < 0)
len = 1;
else
len = 0;
while (num)
{
len++;
num /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
long num;
char *str;
int len;
int i;
num = n;
len = ft_intlen(num);
str = (char *)malloc(len + 1);
if (str == NULL)
return (NULL);
if (num < 0)
{
str[0] = '-';
i = 1;
num *= -1;
}
else
i = 0;
str[len--] = '\0';
while (len >= i)
{
str[len--] = '0' + num % 10;
num /= 10;
}
return (str);
}
// #include <stdio.h>
// int main()
// {
// int num = -123;
// printf("len = %d\n", ft_intlen(num));
// printf("%s\n", ft_itoa(num));
// }