-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
64 lines (58 loc) · 1.53 KB
/
ft_itoa.c
File metadata and controls
64 lines (58 loc) · 1.53 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
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muganiev <gf.black.tv@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/26 15:13:40 by muganiev #+# #+# */
/* Updated: 2022/05/31 18:02:07 by muganiev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_abs(int nbr)
{
if (nbr < 0)
return (-nbr);
else
return (nbr);
}
static void
ft_strrev(char *str)
{
size_t length;
size_t i;
char tmp;
length = ft_strlen(str);
i = 0;
while (i < length / 2)
{
tmp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = tmp;
i++;
}
}
char
*ft_itoa(int n)
{
char *str;
int is_neg;
size_t length;
is_neg = (n < 0);
str = ft_calloc(11 + is_neg, sizeof(*str));
if (!str)
return (NULL);
if (n == 0)
str[0] = '0';
length = 0;
while (n != 0)
{
str[length++] = '0' + ft_abs(n % 10);
n = (n / 10);
}
if (is_neg)
str[length] = '-';
ft_strrev(str);
return (str);
}