-
Notifications
You must be signed in to change notification settings - Fork 0
/
itoa_v0.c
58 lines (44 loc) · 1.05 KB
/
itoa_v0.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
/* Exercise 3-6. Write a version of itoa that accepts three arguments instead
* of two. The third argument is a minimum field width; the converted number
* must be padded with blanks on the left if necessary to make it wide enough.*/
#include "stdio.h"
#include "string.h"
#include "limits.h"
#define MAX 512
#define MIN 64
void itoa(int n, char s[], unsigned lim);
void strrev(char s[]);
int main(void) {
int n = INT_MAX;
char s[MAX];
itoa(n, s, MIN);
printf("%d:\n%s\n", n, s);
return 0;
}
void itoa(int n, char s[], unsigned lim) {
enum signs {POS, NEG};
int sign = n < 0 ? NEG : POS;
size_t i;
unsigned un = n==INT_MIN || n>0 ? n : -n;
i = 0;
do {
s[i++] = un % 2 + '0';
} while ((un >>= 1) > 0);
if (sign == NEG)
s[i++] = '-';
if (i <= MIN)
for (; i < MIN; i++)
s[i] = ' ';
s[i] = '\0';
strrev(s);
}
/* String reverser */
void strrev(char s[]) {
size_t i, j;
char temp;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}