-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecasc.c
More file actions
67 lines (57 loc) · 1.19 KB
/
decasc.c
File metadata and controls
67 lines (57 loc) · 1.19 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
63
64
65
66
67
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "header.h"
#include <math.h>
int getBase10Expo(int num)
{
double temp = num;
int log10Exp = 0;
while (temp >= 10)
{
log10Exp++;
temp /= 10;
}
return log10Exp;
}
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void reverse(char str[], int length) // stolen
{
int start = 0;
int end = length - 1;
while (start < end)
{
swap(*(str + start), *(str + end));
start++;
end--;
}
}
char *itoa(int num, char *str, int base) // Stolen
{
int i = 0;
/* Handle 0 explicitly, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
str[i] = '\0'; // Append string terminator
// Reverse the string
// reverse(str, i);
return str;
}