-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintToString.c
78 lines (71 loc) · 1.55 KB
/
intToString.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
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
/**
* intToString - Converts an integer to a dynamically allocated string.
*
* @number: The integer to be converted to a string.
*
* Description:
* This function takes an integer and converts it
* into a dynamically allocated
* string. The resulting string contains the integer value in string format.
*
* The function allocates memory for the string, ensuring it can accommodate
* the largest possible integer value plus a null-terminator.
*
* If memory allocation fails, the function returns NULL.
*
* Return: A dynamically allocated string containing the integer as a string,
* or NULL on memory allocation failure.
*/
char *intToString(int number)
{
int temp = number;
int length = 0;
char *result;
int loopIndex;
if (temp == 0)
{
length = 1;/* Special case for zero */
}
else
{
while (temp != 0)
{
temp /= 10;
length++;
}
}
/* Allocate memory for the string (including space for null-terminator) */
result = (char *)malloc(length + 1);
if (result == NULL)
{
return (NULL); /* Memory allocation failed */
}
/* Convert the integer to a string manually */
result[length] = '\0'; /* Null-terminate the string */
if (number == 0)
{
result[0] = '0'; /* Special case for zero */
}
else
{
int isNegative = 0;
if (number < 0)
{
isNegative = 1;
number = -number;
}
for (loopIndex = length - 1; loopIndex >= 0; loopIndex--)
{
result[loopIndex] = (char)((number % 10) + '0');
number /= 10;
}
if (isNegative)
{
result[0] = '-'; /* Add a minus sign for negative numbers */
}
}
return (result);
}