-
Notifications
You must be signed in to change notification settings - Fork 0
/
_itoa.c
63 lines (59 loc) · 919 Bytes
/
_itoa.c
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
#include "shell.h"
/**
* _count_number_length - function that
* calculate the length of the given number
*
* @number: to calculate it's length
* Return: length
*/
int _count_number_length(int number)
{
int len;
len = 0;
if (number == 0)
return (1);
if (number < 0)
{
len++;
number *= -1;
}
while (number)
{
len++;
number /= 10;
}
return (len);
}
/**
* _itoa - function convert number int
* string
*
* @number: to be converted
* Return: converted string
*/
char *_itoa(int number)
{
int len;
char *str_number;
len = _count_number_length(number);
str_number = malloc(sizeof(char) * (len + 1));
if (!str_number)
return (NULL);
str_number[len] = 0;
if (!number)
{
str_number[0] = '0';
return (str_number);
}
if (number < 0)
{
str_number[0] = '-';
number *= -1;
}
while (number)
{
str_number[--len] = number % 10 + 48;
number /= 10;
}
return (str_number);
}