-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
88 lines (76 loc) · 1.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julmuntz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/30 15:00:46 by julmuntz #+# #+# */
/* Updated: 2022/06/02 22:50:37 by julmuntz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbrlen(int nbr)
{
int len;
len = 1;
if (nbr < 0)
{
nbr *= -1;
len++;
}
while (nbr >= 10)
{
nbr /= 10;
len++;
}
return (len);
}
char *ft_itoa(int nbr)
{
int size;
char *str;
int sign;
if (nbr == INT_MIN)
return (ft_strdup("-2147483648"));
size = ft_nbrlen(nbr);
str = ft_calloc(size + 1, sizeof(char));
if (str == NULL)
return (NULL);
str[size--] = 0;
sign = 0;
if (nbr < 0)
{
str[0] = '-';
nbr *= -1;
sign++;
}
while (size >= 0 + sign)
{
str[size--] = (nbr % 10) + 48;
nbr /= 10;
}
return (str);
}
/*
#include <stdio.h>
int main(int arc, char **arv)
{
char *var;
if (arc == 2)
{
var = ft_itoa(ft_atoi(arv[1]));
printf("%s\n", var);
free(var);
}
}
*/
/*
static char *ft_intmin(int nbr)
{
char *str;
str = ft_itoa(nbr + 1);
str[10] = '8';
return (str);
}
*/