Extended C programming language exercise 2-3 program versatility, c Programming Language
I recently started to learn C language by myself. I am reading K & R's C programming language. Exercise 2-3 requires a function to convert the input hexadecimal numeric string into an equivalent integer. The matching answer is not universal with the extension program, so I will make a slight transformation.
The answer is ):
1 #define YES 1
2 #define NO 0
3
4 / * htoi: convert hexadecimal digit string to decimal number
5 * If '0x' or '0X' is found then skip and set the starting point of the hexadecimal number
6 * Check if the following characters are 0-9, a-f or A-F
7 * If yes then convert it to integer
8 * Stop checking if not, end of number
9 * Calculate the corresponding decimal value by formula and return
10 * /
11 int htoi (char s [])
12 {
13 int hexdigit, i, inhex, n;
14
15 if (s [i] == '0') {
16 ++ i;
17 if (s [i] == 'x' || s [i] == 'X') {
18 ++ i;
19}
20}
21 n = 0; / * initialize return variable * /
22 inhex = YES; / * Assumes legal characters * /
23 for (; inhex == YES; ++ i) {
24 if (s [i]> = '0' && s [i] <= '9')
25 hexdigit = s [i]-'0';
26 else if (s [i]> = 'a' && s [i] <= 'f')
27 hexdigit = s [i]-'a' + 10;
28 else if (s [i]> = 'A' && s [i] <= 'F')
29 hexdigit = s [i]-'A' + 10;
30 else
31 inhex = NO;
32 if (inhex == YES)
33 n = 16 * n + hexdigit;
34}
35 return n;
36}
This function needs to import an array of characters. I wrote the code for getting the array in the main () function (assuming that you can get an array of up to 10 characters in length, because the array ends with '\ 0', a total of 9 valid characters can be obtained ):
1 int c,i;
2 char input[10];
3
4 for ( i = 0; i < 10-1 && ((c = getchar()) != EOF); ++i)
5 input[i] = c;
6 input[i] = '\0';
Pass the obtained character array to the htoi () function to calculate the converted value.
But there is a flaw: The character array must start with "0x" or "0X" or a number 0-9 or a-f or A-F, otherwise the htoi () function returns only 0. This reduces the versatility of the program, so how can we continue to detect subsequent characters only when "0x" or "0X" is detected when the character array contains useless characters, and calculate and return the correct result?
My approach is to add a loop in the htoi () function to check whether it is "0x" or "0X" code:
1 int htoi (char s [])
2 {
3 int hexdigit, i, inhex, n;
4
5 i = 0;
6 while (s [i]! = '\ 0') {
7 if (s [i] == '0') {
8 ++ i;
9 if (s [i] == 'x' || s [i] == 'X') {
10 ++ i;
11 break; / * Stop loop if "0x" or "0X" * /
12}
13}
14 else
15 ++ i; / * If not, then loop to the end, finally s [i] = '\ 0' * /
16}
17 n = 0;
18 inhex = YES;
19 for (; inhex == YES; ++ i) {
20 ... ...
The running result is as follows:
The complete code will be posted below. If you are lucky enough to let the experts see it, please give pointers:
1 #include <stdio.h>
2
3 #define YES 1
4 #define NO 0
5
6 int htoi (char s []);
7
8 int main (void)
9 {
10 int c, i;
11 char input [10];
12
13 for (i = 0; i <10-1 && ((c = getchar ())! = EOF); ++ i)
14 input [i] = c;
15 input [i] = '\ 0';
16 printf ("The input hexadecimal number is converted to decimal number:% d \ n", htoi (input));
17 return 0;
18}
19
20 int htoi (char s [])
twenty one {
22 int hexdigit, i, inhex, n;
twenty three
24 i = 0;
25 while (s [i]! = '\ 0') {
26 if (s [i] == '0') {
27 ++ i;
28 if (s [i] == 'x' || s [i] == 'X') {
29 ++ i;
30 break;
31}
32}
33 else
34 ++ i;
35}
36 n = 0;
37 inhex = YES;
38 for (; inhex == YES; ++ i) {
39 if (s [i]> = '0' && s [i] <= '9')
40 hexdigit = s [i]-'0';
41 else if (s [i]> = 'a' && s [i] <= 'f')
42 hexdigit = s [i]-'a' + 10;
43 else if (s [i]> = 'A' && s [i] <= 'F')
44 hexdigit = s [i]-'A' + 10;
45 else
46 inhex = NO;
47 if (inhex == YES)
48 n = 16 * n + hexdigit;
49}
50 return n;
51}