forked from steven-schronk/C-Programming-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_4-12.c
More file actions
69 lines (60 loc) · 1.18 KB
/
ex_4-12.c
File metadata and controls
69 lines (60 loc) · 1.18 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
68
69
#include <string.h>
#include <stdio.h>
/* print contents of array */
void print_array(char s[])
{
int i;
for(i = 0; i < strlen(s); i++)
printf("%c", s[i]);
printf("\n");
}
/* reverse contents of array in place */
void reverse_array(char s[])
{
int c, i, j;
for(i = 0, j = strlen(s)-1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* convert int n into chars in s[] - page 64*/
void itoa(int n, char s[])
{
int i, sign;
if((sign = n) < 0) // record sign
n = -n; // reverse sign
i = 0;
do { // generates digits in reverse order
s[i++] = n % 10 + '0'; // get next digit and load into array
} while ((n /= 10) > 0); // delete each digit as we move along
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse_array(s);
}
/* recursive version of itoa - book has not covered pointers yet :( */
int itoa_rec(int n, char s[], int i)
{
if(n < 0)
{
s[i++] = '-';
n = -n;
}
if(n / 10)
i = itoa_rec(n/10, s,i);
s[i++] = (n % 10 + '0');
s[i] = '\0';
return i;
}
int main()
{
char s[10]; // must have plenty of space here...
int n = -1024;
itoa(n, s);
print_array(s);
itoa_rec(n, s, 0);
print_array(s);
return 1;
}