C programming language-Chapter 2-exercises, C language programming exercises
As there are many excellent solutions on the Internet for Chapter 1, I will not post them. You may wish to explore them on your own.
Exercise 3-2 compile a function escape (s, t) and copy string t to string s, during the copy process, invisible characters such as line breaks and tabs are converted into visible escape character sequences such as '\ n' and' \ t. The switch statement is required. Compile a function with the opposite function, and convert the Escape Character Sequence to the actual character during the copy process.
The two run functions and two escape functions are described as follows:
1 #include <stdio.h>
2
3 void escape(char s[], char t[]);
4 void escape2(char s[], char t[]);
5
6 void runEscape() {
7 char testT[100] = "Hello,\ni am a\nGOOD\tBOY\t!!!!\n";
8 char testS[100] = "Bye,\t littal Max!\n";
9
10 printf("T:%sS:%s", testT, testS);
11 escape(testS, testT);
12 printf("Now,");
13 printf("T:%sS:%s", testT, testS);
14 }
15 void runEscape2() {
16 char testS[100] = "Hello,\ni am a\nGOOD\tBOY\t!!!!\n";
17 char testT[100] = "Bye,\t littal Max!\n";
18
19 printf("T:%sS:%s", testT, testS);
20 escape2(testS, testT);
21 printf("Now,");
22 printf("T:%sS:%s", testT, testS);
23 }
24 void escape(char s[], char t[]) {
25 int i = 0;
26 int j = 0;
27
28 while (t[i] != '\0') {
29 switch (t[i])
30 {
31 case '\t':
32 s[j++] = '\\';s[j++] = 't';
33 break;
34 case '\n':
35 s[j++] = '\\'; s[j++] = 'n';
36 break;
37 default:
38 s[j++] = t[i];
39 break;
40 }
41
42 i++;
43 }
44 s[j] = '\0';
45 }
46 void escape2(char s[], char t[]) {
47 int i = 0;
48 int j = 0;
49
50 while (t[i] != '\0') {
51 if (t[i]=='\\')
52 {
53 switch (t[i+1])
54 {
55 case 't':
56 s[j++] = '\t'; i++;
57 break;
58 case 'n':
59 s[j++] = '\n'; i++;
60 break;
61 default:
62 s[j++] = t[i];
63 break;
64 }
65 }
66 else
67 s[j++] = t[i];
68 i++;
69 }
70 s[j] = '\0';
71 }
Exercise 3-3 compile the function expand (s1, s2) and extend the stenographer in string s1, similar to a-z, into a complete list of equivalent values in string s2... xyz. This function can handle uppercase and lowercase letters and numbers, and can handle a-B-c, a-z0-9 And-a-z and other similar situations. As the leading and trailing-character is outprinted.
1 #include <stdio.h>
2
3 void expand(char s1[], char s2[]);
4
5 void runExpand() {
6 char testS[100] = "0-9\na-z\nA-Z\na-b-c\na-a\na-Z\n";
7 char testT[100] = "Bye, littal Tester!\n";
8
9 printf("T:%sS:%s", testT, testS);
10 expand(testS, testT);
11 printf("\nNow,\n");
12 printf("T:%sS:%s", testT, testS);
13 }
14
15
16 void expand(char s1[], char s2[]) {
17 int i = 0;
18 int j = 0;
19
20 while (s1[i] != '\0') {
21 if (s1[i] == '-'&&s1[i + 1] != '\0' && i > 0) {
22 char head = s1[i - 1];
23 char tail = s1[i + 1];
24
25 if ('a' <= head&&head <= 'z'&&head < tail
26 &&'a' <= tail&&tail <= 'z') {
27 for (; head + 1 <= tail;) {
28 s2[j++] = head + 1;
29 head++;
30 }
31 }
32 else if ('A' <= head&&head <= 'Z'&&head < tail
33 &&'A' <= tail&&tail <= 'Z') {
34 for (; head + 1 < tail;) {
35 s2[j++] = head + 1;
36 head++;
37 }
38 }
39 else if ('0' <= head&&head <= '9'&&head < tail
40 &&'0' <= tail&&tail <= '9') {
41 for (; head + 1 < tail;) {
42 s2[j++] = head + 1;
43 head++;
44 }
45 }
46 else
47 s2[j++] = '-';
48 }
49 else
50 s2[j++] = s1[i];
51
52 i++;
53 }
54 }
In the 2nd complement representation of the numbers in Exercise 3-4, the itoa function we compile cannot process the maximum negative number, that is, the case where n is equal to-2 characters long-1. Please explain the reason. Modify the function so that it can print the correct value when running on any machine.
I wrote the explanation in the comments code ~~
#include <stdio.h>
void reverse (char s []);
void itoa (int n, char s []);
void runItoa () {
int testn = -1987654321;
char test [100];
itoa (testn, test);
printf ("test:% s", test);
}
/ * The reason why the largest negative number cannot be handled correctly is that its binary number form is 10000 ... if
Performing n = -n operation on it, n will become 0, which obviously does not meet our expectations. so I
We can detect it and turn it into an unsigned number if it is this particular largest negative number. * /
void itoa (int n, char s []) {
int i, sign;
sign = n;
if (((unsigned) sign << 1) == 0)
n = (unsigned) n;
else if (sign <0)
n = -n;
i = 0;
do {
s [i ++] = n% 10 + '0';
} while ((n / = 10)> 0);
if (sign <0)
s [i ++] = '-';
s [i] = '\ 0';
reverse (s);
}
void reverse (char s []) {
int i = 0;
int j = 0;
char c [1000];
while (s [i]! = '\ 0')
c [i] = s [i ++];
while (i> 0)
s [j ++] = c [-i];
s [j] = '\ 0';
}
Exercise 3-5 compile the itob (n, s, B) function to convert the integer n to the base number of B. And save the conversion result as a character to string s.
#include <stdio.h>
void reverse(char s[]);
void itob(int n, char s[], int);
void runItob() {
int testn = 1987654321;
char test[100];
itob(testn, test, 10);
printf("test:%s", test);
}
void itob(int n, char s[], int b) {
int i = 0;
do {
s[i++] = n % b + '0';
} while ((n /= b) > 0);
s[i] = '\0';
reverse(s);
}
Exercise 3-6 modify the itoa function so that the function can receive three parameters. The third parameter is the minimum field width. To ensure that the converted result has at least the minimum width specified by the third parameter, enter a certain space on the left of the result if necessary.
1 #include <stdio.h>
2
3 void reverse(char s[]);
4 void itoa2(int n, char s[], int width);
5
6 void runItoa2() {
7 int testn = -1987654321;
8 char test[100];
9
10 itoa2(testn, test,20);
11 printf("test:%send\n", test);
12 }
13
14 void itoa2(int n, char s[], int width) {
15 int i, sign;
16 sign = n;
17
18 if (((unsigned)sign << 1) == 0)
19 n = (unsigned)n;
20 else if (sign < 0)
21 n = -n;
22 i = 0;
23 do {
24 s[i++] = n % 10 + '0';
25 } while ((n /= 10) > 0);
26 if (sign < 0)
27 s[i++] = '-';
28 while (width - i > 0) {
29 s[i++] = ' ';
30 }
31 s[i] = '\0';
32 reverse(s);
33 }