forked from thesstefan/The-C-Programming-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-12.c
More file actions
62 lines (47 loc) · 1.24 KB
/
4-12.c
File metadata and controls
62 lines (47 loc) · 1.24 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void itoa(int number, char digit_string[]) {
static int index = 0;
if (number < 0) {
digit_string[index] = '-';
number = -number;
}
if (number / 10)
itoa(number / 10, digit_string);
else
index = 0;
digit_string[++index] = abs(number) % 10 + '0';
digit_string[index] = '\0';
}
void itoa2_operation(int number, char digit_string[], int index) {
if (number / 10) {
itoa2_operation(number / 10, digit_string, --index);
} else if (digit_string[0] == '-')
index = 1;
else
index = 0;
digit_string[index] = abs(number) % 10 + '0';
digit_string[++index] = '\0';
}
void itoa2(int number, char digit_string[]) {
int digits = 0;
int number_copy = -number;
while (number_copy != 0) {
number_copy /= 10;
digits++;
}
if (number < 0) {
digit_string[0] = '-';
number = -number;
itoa2_operation(number, digit_string, digits + 1);
} else
itoa2_operation(number, digit_string, digits);
}
int main() {
int number = 23423;
char digit_string[100];
itoa2(number, digit_string);
printf("\n%s", digit_string);
return 0;
}