-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
74 lines (67 loc) · 1.67 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
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acalin-b <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/19 14:33:30 by acalin-b #+# #+# */
/* Updated: 2023/03/22 10:46:37 by acalin-b ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <stdlib.h>
int ft_intlen(int n)
{
int i;
i = 0;
while (n > 0)
{
n = n / 10;
i++;
}
return (i);
}
char *hardcodrd(int n)
{
if (n == 0)
return (ft_strdup("0"));
if (n == -2147483648)
return (ft_strdup("-2147483648"));
return (NULL);
}
char *ft_itoa(int n)
{
int neg;
char *str;
int len;
neg = 0;
if (n == 0 || n == -2147483648)
return (hardcodrd(n));
if (n < 0)
{
neg = 1;
n = -n;
}
len = ft_intlen(n);
str = (char *)malloc(sizeof(char) * (len + neg + 1));
if (!str)
return (NULL);
str[len + neg] = '\0';
while (len--)
{
str[len + neg] = (n % 10) + '0';
n /= 10;
}
if (neg)
str[0] = '-';
return (str);
}
//int main(void)
//{
// char *str = ft_itoa(-9874);
// printf("RESULT: %s ", str);
// free(str);
// return (0);
//}