-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-e14.c
54 lines (40 loc) · 1.1 KB
/
10-e14.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
// convert integer to string
#include <stdio.h>
#include <stdbool.h>
char string[] = "";
int main(void) {
void intToStr(int num);
int nums[] = {0, -1, 10, -23, 100, -456, 1000, -78900}, j;
for (int i = 0; i < 8; i++) {
intToStr(nums[i]);
printf("int: %i\nstr: %s\n\n", nums[i], string);
}
return 0;
}
void intToStr(int num) { // convert an integer to a string
int digits = 0, digit, digitInt, digitCount, i, j = 0;
bool isNegative = false;
if (num < 0) {
isNegative = true;
num *= -1;
j++;
string[0] = '-';
}
if (num == 0) {
digitCount = 1;
string[0] = '0';
} else {
for (int tmp = num; tmp > 0; digits++) tmp /= 10; // number of digits
digitCount = digits;
while(digits--) { // iterate through digits
i = 1;
digit = digits;
while (digit--) i *= 10;
digitInt = num / i % 10;
string[j] = 48 + digitInt;
j++;
}
}
if (isNegative) string[digitCount + 1] = '\0';
else string[digitCount] = '\0';
}