-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
42 lines (39 loc) · 1.29 KB
/
ft_itoa.c
File metadata and controls
42 lines (39 loc) · 1.29 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: enikel <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/27 12:41:40 by enikel #+# #+# */
/* Updated: 2018/06/14 08:35:31 by enikel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
int i;
long int num;
num = (long int)n;
i = ft_intlen(num) - 1;
if (num < 0)
{
i++;
num = num * -1;
}
str = (char *)malloc(i + 2);
if (!str)
return (NULL);
if (num == 0)
str[0] = '0';
str[i + 1] = '\0';
while (num > 0)
{
str[i--] = (char)((num % 10) + 48);
num = num / 10;
}
if (i == 0 && str[i + 1] > 0)
str[i] = '-';
return (str);
}