-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
48 lines (43 loc) · 1.31 KB
/
ft_itoa.c
File metadata and controls
48 lines (43 loc) · 1.31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nhennigh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/12 20:27:06 by nhennigh #+# #+# */
/* Updated: 2019/02/15 13:01:19 by nhennigh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_len(int n)
{
int len;
len = 1;
while (n /= 10)
len++;
return (len);
}
char *ft_itoa(int n)
{
char *str;
int len;
unsigned int n_cpy;
len = get_len(n);
n_cpy = n;
if (n < 0)
{
n_cpy = -n;
len++;
}
if (!(str = ft_strnew(len)))
return (NULL);
str[--len] = n_cpy % 10 + '0';
while (n_cpy /= 10)
{
str[--len] = n_cpy % 10 + '0';
}
if (n < 0)
*(str + 0) = '-';
return (str);
}