-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
53 lines (48 loc) · 1.44 KB
/
ft_itoa.c
File metadata and controls
53 lines (48 loc) · 1.44 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
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_itoa.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: thperchi <marvin@le-101.fr> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/07/14 22:22:40 by thperchi #+# ## ## #+# */
/* Updated: 2018/10/12 00:31:03 by thperchi ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static int ft_len(int n)
{
int x;
x = 0;
while (n /= 10)
x++;
return (x);
}
char *ft_itoa(int n)
{
char *str;
int x;
int y;
x = ft_len(n);
y = n;
if (y == -2147483648)
{
str = ft_strdup("-2147483648");
return (str);
}
if (n < 0)
{
y = -n;
x++;
}
if (!(str = (char *)malloc(sizeof(char) * (x + 2))))
return (NULL);
str[x + 1] = '\0';
str[x] = y % 10 + '0';
while (y /= 10)
str[--x] = y % 10 + '0';
if (n < 0)
str[0] = '-';
return (str);
}