-
Notifications
You must be signed in to change notification settings - Fork 0
/
itoa.c
75 lines (63 loc) · 1.06 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
int number_of_digits(unsigned int d);
/**
* _itoa - converts an int to a string
* @a: number to be converted
*
* Return: pointer to string that can be freed using malloc or NULL on error
*/
char *_itoa(int a)
{
char *string, *p;
int rem, quo, size_of_string;
rem = 0;
quo = _abs(a);
size_of_string = number_of_digits((unsigned int) a);
if (a < 0)
size_of_string++;
string = malloc(sizeof(char) * (size_of_string + 1));
if (!string)
return (NULL);
if (a == 0)
{
string[0] = '0';
string[1] = '\0';
return (string);
}
p = string;
while ((quo / 10 != 0) || (quo % 10 != 0))
{
rem = quo % 10;
*p = rem + '0';
quo /= 10;
p++;
}
if (a < 0)
{
*p = '-';
p++;
}
*p = '\0';
reverse_string(string);
return (string);
}
/**
* number_of_digits - returns number of digits for an int
* @d: number
*
* Return: number of digits
*/
int number_of_digits(unsigned int d)
{
int count = 0;
if (d == 0)
return (1);
while (d / 10 != 0)
{
count++;
d /= 10;
}
return (count + 1);
}