Skip to content

Commit 704ce4c

Browse files
committed
finished Chapter-3
1 parent 6bf04a7 commit 704ce4c

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

Chapter-3/Exercise 3-4/itoa.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <limits.h>
4+
#define MAXLENGTH 100
5+
6+
void itoa(int number, char string[]);
7+
void reverse(char str[]);
8+
9+
int main() {
10+
char buffer[MAXLENGTH];
11+
int num = INT_MIN;
12+
itoa(num, buffer);
13+
printf("%s\n", buffer);
14+
return 0;
15+
}
16+
17+
void itoa(int n, char s[]) {
18+
int i, sign = 1;
19+
unsigned num;
20+
if(n < 0) {num = -n; sign = -1;}
21+
i = 0;
22+
do {
23+
s[i++] = num % 10 + '0';
24+
} while ((num /= 10) > 0);
25+
26+
if (sign < 0) s[i++] = '-';
27+
s[i] = '\0';
28+
reverse(s);
29+
}
30+
31+
void reverse(char s[]) {
32+
int c, i, j;
33+
for (i = 0, j = strlen(s)-1; i < j ; i++, j--) {
34+
c = s[j], s[j] = s[i], s[i] = c;
35+
}
36+
}

Chapter-3/Exercise 3-4/itoa_v2.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <limits.h>
4+
#include <stdlib.h>
5+
#define MAXLENGTH 100
6+
7+
void itoa(int number, char string[]);
8+
void reverse(char str[]);
9+
10+
int main() {
11+
char buffer[MAXLENGTH];
12+
int num = INT_MIN;
13+
itoa(num, buffer);
14+
printf("%s\n", buffer);
15+
return 0;
16+
}
17+
18+
void itoa(int n, char s[]) {
19+
int i, sign;
20+
sign = n;
21+
i = 0;
22+
do {
23+
s[i++] = abs(n % 10) + '0';
24+
} while (n /= 10);
25+
26+
if (sign < 0) s[i++] = '-';
27+
s[i] = '\0';
28+
reverse(s);
29+
}
30+
31+
void reverse(char s[]) {
32+
int c, i, j;
33+
for (i = 0, j = strlen(s)-1; i < j ; i++, j--) {
34+
c = s[j], s[j] = s[i], s[i] = c;
35+
}
36+
}

Chapter-3/Exercise 3-5/itob.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <limits.h>
4+
#define MAXLENGTH 1000
5+
6+
int itob(int num, char str[], int base);
7+
void reverse(char string[]);
8+
9+
int main() {
10+
char encoded[MAXLENGTH];
11+
int num = INT_MAX;
12+
int base = 62;
13+
if (itob(num, encoded, base) != -1) printf("%s\n", encoded);
14+
return 0;
15+
}
16+
17+
int itob(int n, char s[], int b) {
18+
static const char CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
19+
if (b < 2 || b > 62) return -1;
20+
int i = 0;
21+
do {
22+
s[i++] = CHARS[n % b];
23+
} while (n /= b);
24+
s[i] = '\0';
25+
reverse(s);
26+
}
27+
28+
void reverse(char s[]) {
29+
int c, i, j;
30+
for (i = 0, j = strlen(s)-1; i < j ; i++, j--) {
31+
c = s[j], s[j] = s[i], s[i] = c;
32+
}
33+
}

Chapter-3/Exercise 3-6/itoa.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <limits.h>
4+
#include <stdlib.h>
5+
#define MAXLENGTH 100
6+
7+
void itoa(int number, char string[], int field_width);
8+
void reverse(char str[]);
9+
10+
int main() {
11+
char buffer[MAXLENGTH];
12+
int num = INT_MIN;
13+
int width = 15;
14+
itoa(num, buffer, width);
15+
printf("%s\n", buffer);
16+
return 0;
17+
}
18+
19+
void itoa(int n, char s[], int w) {
20+
int i, sign;
21+
sign = n;
22+
i = 0;
23+
do {
24+
s[i++] = abs(n % 10) + '0';
25+
} while (n /= 10);
26+
27+
if (sign < 0) s[i++] = '-';
28+
while (w - i > 0) s[i++] = ' ';
29+
s[i] = '\0';
30+
reverse(s);
31+
}
32+
33+
void reverse(char s[]) {
34+
int c, i, j;
35+
for (i = 0, j = strlen(s)-1; i < j ; i++, j--) {
36+
c = s[j], s[j] = s[i], s[i] = c;
37+
}
38+
}

0 commit comments

Comments
 (0)