-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
82 lines (75 loc) · 2.12 KB
/
ft_itoa.c
File metadata and controls
82 lines (75 loc) · 2.12 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
73
74
75
76
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aguiri <aguiri@student.42nice.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/29 01:56:53 by papy #+# #+# */
/* Updated: 2021/10/31 11:10:33 by aguiri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* \brief Return the length of a given integer.
*
* \param n Integer to be counted.
* \return Length of the integer. +1 if the number appends to be negative.
*/
static int ft_intlen(long n)
{
int i;
i = 1;
if (n < 0)
i++;
while (n > 10)
{
n = n / 10;
i++;
}
return (i);
}
/**
* \brief Write an int number in a dedicated string. Recursive function.
*
* \param i Current position in the string.
* \param str String to write into.
* \param n Number to be written.
*/
static void ft_write_numb(int *i, char *str, long n)
{
if (n >= 10)
{
ft_write_numb(i, str, n / 10);
ft_write_numb(i, str, n % 10);
}
else
str[(*i)++] = n + '0';
}
/**
* \brief Allocate (with malloc()) and returns a string of characters
* representing the integer received as argument.
* Negative numbers must be handled.
*
* \param n Integer to be converted.
* \return String that represent the integer, NULL if the allocation fails.
*/
char *ft_itoa(int n)
{
int i;
long n_long;
char *out;
n_long = n;
out = malloc(sizeof(char) * (ft_intlen(n_long) + 1));
if (!out)
return (NULL);
i = 0;
if (n_long < 0)
{
out[i++] = '-';
n_long *= -1;
}
ft_write_numb(&i, out, n_long);
out[i] = '\0';
return (out);
}