-
Notifications
You must be signed in to change notification settings - Fork 1
/
_IntToStr.c
68 lines (63 loc) · 1005 Bytes
/
_IntToStr.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
#include "shell.h"
/**
* reverse - function that reverse a string
* @str: string to reverse
* @len: len of string
* Return: int number of characters
* On error, return 0
*/
void reverse(char *str, int len)
{
int i = 0, j = len - 1, temp;
if (str[i] == '-')
i++;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
/**
* intToStr - function that convert a int to string
* @x: integer to convert to string
* @str: array that containt string result
* Return: length string result
* On error, return 0
*/
int intToStr(int x, char str[])
{
int i = 0;
if (x == INT_MIN)
{
str[0] = '-';
str[1] = '2';
str[2] = '1';
str[3] = '4';
str[4] = '7';
str[5] = '4';
str[6] = '8';
str[7] = '3';
str[8] = '6';
str[9] = '4';
str[10] = '8';
str[11] = '\0';
return (11);
}
if (x < 0)
{
x = abs(x);
str[i] = '-';
i++;
}
while (x)
{
str[i++] = (x % 10) + '0';
x = x / 10;
}
reverse(str, i);
str[i] = '\0';
return (i);
}