-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
79 lines (69 loc) · 1.39 KB
/
Copy pathft_itoa.c
File metadata and controls
79 lines (69 loc) · 1.39 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
The ft_itoa function performs the inverse operation of ft_atoi, converting a
numerical value into its corresponding string representation.
ft_count_digits():
Counts number of digits (incl. '-') in an int. The purpose is to determine the
number of characters needed to represent the integer as a string.
ft_itoa():
Allocates memory for the string based on the count of digits plus one additional
char for '\0'. Converts 'int' to 'char' digit by digit from right to left and
fills the string accordingly.
Handles special cases "INT_MIN" (as (INT_MIN *= -1) > INT_MAX) and '0' by
directly creating the string via ft_strdup().
*/
#include "libft.h"
static int ft_count_digits(int n)
{
int i;
i = 0;
if (n < 0)
{
n *= -1;
i++;
}
while (n > 0)
{
i++;
n = n / 10;
}
return (i);
}
char *ft_itoa(int n)
{
char *str;
size_t i;
i = ft_count_digits(n);
if (n == 0)
return (ft_strdup("0"));
if (n == -2147483648)
return (ft_strdup("-2147483648"));
str = malloc((i + 1) * sizeof(char));
if (!str)
return (NULL);
str[i] = '\0';
i--;
if (n < 0)
{
str[0] = '-';
n *= -1;
}
while (n > 0)
{
str[i] = (n % 10) + '0';
n /= 10;
i--;
}
return (str);
}
/*
#include <stdio.h>
int main(void)
{
printf("%s\n", ft_itoa(12345));
printf("%s\n", ft_itoa(-12345));
printf("%s\n", ft_itoa(0));
printf("%s\n", ft_itoa(-2147483648));
printf("%s\n", ft_itoa(2147483647));
return (0);
}
*/