-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
72 lines (65 loc) · 1.73 KB
/
ft_itoa.c
File metadata and controls
72 lines (65 loc) · 1.73 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmoutaou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/14 14:48:39 by kmoutaou #+# #+# */
/* Updated: 2021/11/19 16:36:19 by kmoutaou ### ########.fr */
/* */
/* ************************************************************************** */
/*
ft_itoa is a function that allocates and returns a string representing
the integer received as an argument.
*/
#include "libft.h"
static int ft_size(int n)
{
int size;
size = 1;
while (n > 9)
{
n = n / 10;
size++;
}
return (size);
}
static char *ft_convert(char *str, int n, int index, int sign)
{
if (sign)
str[0] = '-';
while (n > 0)
{
str[index] = (n % 10) + '0';
n = n / 10;
index--;
}
return (str);
}
char *ft_itoa(int n)
{
char *result;
int sign;
int size;
if (n == -2147483648)
{
result = ft_strdup("-2147483648");
return (result);
}
sign = 0;
if (n < 0)
{
n = n * -1;
sign = 1;
}
size = ft_size(n) + sign;
result = (char *)malloc(sizeof(char) * (size + 1));
if (!result)
return (result);
if (n == 0)
result[0] = '0';
result = ft_convert(result, n, size - 1, sign);
result[size] = '\0';
return (result);
}