-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3-6.c
50 lines (43 loc) · 996 Bytes
/
ex3-6.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
#include <stdio.h>
#include <string.h>
#include <limits.h>
/* reverse: reverse string s in place */
void reverse(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;
}
}
/* another 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 */
void itoa(int n, char s[], int width) {
int i = 0, sign = n;
unsigned value;
int blanks;
if (n == INT_MIN) {
value = (unsigned) INT_MAX + 1;
}
else if (n < 0) {
value = -n;
}
else {
value = n;
}
do {
s[i++] = value % 10 + '0';
} while ((value /= 10) > 0);
if (sign < 0)
s[i++] = '-';
for (blanks = width - i; blanks > 0; blanks--) {
s[i++] = ' ';
}
s[i] = '\0';
reverse(s);
}
int main() {
int n = 196;
char s[30];
itoa(n,s,2);
printf("%s\n", s);
}