forked from AingeruAlvarezSanchez/Libft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_itoa.c
72 lines (67 loc) · 1.83 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aalvarez <aalvarez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/17 18:30:13 by aalvarez #+# #+# */
/* Updated: 2022/08/17 19:58:11 by aalvarez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
/**
* @brief calculates the size of the allocation
* needed for the string.
*
* @param n integer to be representd as a string.
* @return int the size needed to the allocation,
* this funtion does not count the NULL terminating
* byte.
*/
static int ft_arrsize(int n)
{
int size;
size = 0;
if (n <= 0)
size++;
while (n)
{
size++;
n /= 10;
}
return (size);
}
/**
* @brief converts the integer
* n to its string representation.
*
* @param n integer to be represented as a string.
* @return char* result of the string representation of n.
*/
char *ft_itoa(int n)
{
char *result;
int size;
long nbr;
size = ft_arrsize(n);
nbr = n;
result = (char *) malloc(sizeof(char) * (size + 1));
if (!result)
return (NULL);
if (nbr < 0)
{
result[0] = '-';
nbr *= -1;
}
if (!nbr)
result[0] = '0';
result[size--] = 0;
while (nbr)
{
result[size--] = (nbr % 10) + '0';
nbr /= 10;
}
return (result);
}