-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
61 lines (55 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/17 13:57:19 by bnidia #+# #+# */
/* Updated: 2021/10/26 17:54:52 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
/* ft_itoa
** Allocates (with malloc(3)) and returns a string representing the integer
** received as an argument. Negative numbers must be handled.
** Return Value
** The string representing the integer. NULL if the allocation fails.
** Comment
** max int 2,147,483,647 - 10b + '-' + '/0' = 12b
*/
#include "libft.h"
static int ft_lenint(int n)
{
int i;
i = 0;
if (n <= 0)
i++;
while (n)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *str;
long num;
int lenint;
num = (long)n;
lenint = ft_lenint(n);
str = (char *)malloc(lenint + 1);
if (!str)
return (NULL);
if (num < 0)
num = -num;
str[lenint] = '\0';
while (lenint--)
{
str[lenint] = num % 10 + '0';
num /= 10;
}
if (n < 0)
str[0] = '-';
return (str);
}