程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 《C和指針》章節後編程練習解答參考——第9章,《c和指針》第9章

《C和指針》章節後編程練習解答參考——第9章,《c和指針》第9章

編輯:關於C語言

《C和指針》章節後編程練習解答參考——第9章,《c和指針》第9章


9.1

 

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <string.h>
 4 
 5 #define  N  100
 6 
 7 int main (void)
 8 {
 9     int i = 0;
10     char strarray[N+1]; //= "Hello world!    \t3I'm here!";
11     char *input = strarray;
12     char ch;
13     unsigned int cont = 0;
14     int ctr=0, spc=0, num=0, low=0, upp=0, puc=0, upr=0;
15     float ctrf=0, spcf=0, numf=0, lowf=0, uppf=0, pucf=0, uprf=0;
16 
17     printf("Please enter a string:\n");
18 
19     while (( ch = getchar() ) != '\n')
20     {    
21         *(input + i) = ch;
22         i++;    
23     }
24     *(input + i) = '\0';
25     cont = strlen(input);
26 
27     while (*input != '\0') 
28     {
29         if (iscntrl(*input))
30             ctr++;
31         if (isspace(*input))
32             spc++;
33         if (isdigit(*input))
34             num++;
35         if (islower(*input))
36             low++;
37         if (isupper(*input))
38             upp++;
39         if (ispunct(*input))
40             puc++;
41         if (!(isprint(*input)))
42             upr++;
43         input++;
44     }
45 
46     printf("\nTotle char number is: Count = %d\n", cont);
47     printf("cntrl = %d,\tspace = %d,\tdigit = %d\n", ctr, spc, num);
48     printf("lower = %d,\tupper = %d,\tpunct = %d\n", low, upp, puc);
49     printf("uprint = %d\n", upr);
50 
51     ctrf = (float)ctr/(float)cont;
52     spcf = (float)spc/(float)cont;
53     numf = (float)num/(float)cont;
54     lowf = (float)low/(float)cont;
55     uppf = (float)upp/(float)cont;
56     pucf = (float)puc/(float)cont;
57     uprf = (float)upr/(float)cont;
58 
59     printf("\nHere is the percent of every count:\n");
60     printf("cntrl = %.3f%%\nspace = %.3f%%\ndigit = %.3f%%\n",ctrf*100 , spcf*100, numf*100);
61     printf("lower = %.3f%%\nupper = %.3f%%\npunct = %.3f%%\n", lowf*100, uppf*100, pucf*100);
62     printf("uprnt = %.3f%%\n", uprf*100);
63 
64     getchar();
65     return 0;
66 }

 

 

9.3

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void my_strcpy(char *dst, char *src)
 5 {
 6     size_t len = 0, dstlen = 0, srclen = 0;
 7     dstlen = strlen(dst);
 8     srclen = strlen(src);
 9 
10     len = ( dstlen >= srclen ? srclen : dstlen );
11     strncpy(dst, src, len);
12 }
13 
14 void show_string(char *str)
15 {
16     while (*str != '\0')
17         printf("%c", *str++);
18     putchar('\n');
19 }
20 
21 int main (void)
22 {
23     char dst[6] = "Hello";
24     char src[10] = "I'm here!";
25     char *dstp = dst;
26     char *srcp = src;
27 
28     my_strcpy(dstp, srcp);
29 
30     show_string(dstp);
31     show_string(srcp);
32 
33     getchar();
34     return 0;
35 }

 

 

9.4

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define  M  12
 5 #define  N  16
 6 
 7 void my_strcat(char *dst, char *src)
 8 {
 9     size_t len = 0, dstlen = 0, srclen = 0, restlen = 0;
10     dstlen = strlen(dst);
11     srclen = strlen(src);
12     
13     len = ( (restlen = M - dstlen - 1) > srclen ? srclen : restlen );
14     strncat(dst, src, len);
15 }
16 
17 void show_string(char *str)
18 {
19     while (*str != '\0')
20         printf("%c", *str++);
21     putchar('\n');
22 }
23 
24 int main (void)
25 {
26     char dst[M] = "Hello!";
27     char src[N] = "I'm here!";
28     char *dstp = dst;
29     char *srcp = src;
30 
31     my_strcat(dst, src);
32 
33     show_string(dstp);
34     show_string(srcp);
35 
36     getchar();
37     return 0;
38 }

 

 

9.5

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define  M  12
 5 #define  N  16
 6 
 7 void my_strncat(char *dest, char *src, int dest_len)
 8 {
 9     size_t len = 0, dstlen = 0, srclen = 0, restlen = 0;
10     dstlen = strlen(dest);
11     srclen = strlen(src);
12     
13     len = ( (restlen = dest_len - dstlen - 1) > srclen ? srclen : restlen );
14     strncat(dest, src, len);
15 }
16 
17 void show_string(char *str)
18 {
19     while (*str != '\0')
20         printf("%c", *str++);
21     putchar('\n');
22 }
23 
24 int main (void)
25 {
26     char dst[M] = "Hello!";
27     char src[N] = "I'm here!";
28     char *dstp = dst;
29     char *srcp = src;
30 
31     my_strncat(dst, src, M);
32 
33     show_string(dstp);
34     show_string(srcp);
35 
36     getchar();
37     return 0;
38 }

 

 

9.6

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char *my_strcpy_end(char *dst, char *src)
 5 {
 6     int i, dstlen = 0, srclen = 0;
 7     
 8     dstlen = strlen(dst);
 9     srclen = strlen(src);
10 
11     if ( dstlen >= srclen )
12         strcpy(dst, src);
13     else
14         strncpy(dst, src, dstlen);
15 
16     for ( i = 0; *(dst + i) != '\0'; i++);
17     return dst + i;
18 }
19 
20 int main (void)
21 {
22     char *p = NULL;
23     char dst[] = "Hello world";
24     //char src[] = "123456789";
25     char src[] = "123456789123456789";
26 
27     p = my_strcpy_end(dst, src);
28 
29     printf("%c\n", *(p - 1));
30 
31     getchar();
32     return 0;
33 }


9.8

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char *my_strnchr( char const *str, int ch, int which )
 5 {
 6     int i;
 7     char *p = NULL;
 8     p = str;
 9 
10     for ( i = 0; i < which; i++)
11     {
12         if ( i != 0)
13             p++;
14         p = strchr(p, ch);
15     }
16 
17     return p;
18 }
19 
20 int main (void)
21 {
22     int i;
23     char str[] = "Hello world!";
24     int chr = 'l';
25     int which = 2;
26 
27     char *find = my_strnchr(str, chr, which);
28 
29     for (i = -1; i < 2; i++)
30         printf("%c\t", *(find + i));
31     putchar('\n');
32 
33     getchar();
34     return 0;
35 }

 

9.9

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int count_chars(char const *str, char const *chars)
 5 {
 6     int count = 0, len = 0;
 7     char *pos = NULL;
 8     pos = str;
 9 
10     while (( pos = strpbrk(pos, chars) ) != NULL )
11     {
12         count++;
13         pos++;
14     }
15 
16     return count;
17 }
18 
19 int main (void)
20 {
21     char str[] = "Hello world!";
22     char chars[] = "ero";
23 
24     printf("%d\n", count_chars(str, chars));
25 
26     getchar();
27     return 0;
28 }

 

 

9.10

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <ctype.h>
 5 
 6 int palindrome( char *string )
 7 {
 8     int i, j = 0;
 9     int len = strlen(string);
10     char *strcopy = NULL;
11     strcopy = malloc((len + 1)* sizeof(char));
12     for (i = 0; i < len; i++)            //將源字符串中的非字母字符去掉,並將剩下的字母轉換為小寫
13     {
14         if (isalpha(*(string + i)))
15         {
16             *(strcopy + j) =  tolower( *(string + i) );
17             j++;
18         }
19     }
20     *(strcopy + j) = '\0';
21     
22     len = strlen(strcopy);
23     for (i = 0; i < (len / 2); i++)
24     {
25         if ( (*(strcopy + i)) != (*(strcopy + (len - i - 1) ) ) )
26             return 0;
27     }
28 
29     return 1;
30 }
31 
32 int main (void)
33 {
34     char string[] = "Madam, I'm Adam!";
35     int result = 0;
36 
37     result = palindrome(string);
38     printf("%d\n", result);
39 
40     getchar();
41     return 0;
42 }

 

 

9.11

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main (void)
 5 {
 6     char input[101];
 7     char chars[] = "the";
 8     int i = 0, count = 0;
 9     char *pos = NULL;
10 
11     printf("Please enter some words: \n");
12     scanf("%s", input);
13     printf("%s\n", input);
14 
15     pos = input;
16     while ((pos = strstr(pos, chars)) != NULL)
17     {
18         pos++;
19         count++;
20     }
21 
22     printf("There are %d \"%s\"!", count, chars);
23 
24     getchar();
25     getchar();
26     return 0;
27 }

 

 

9.12  9.13  9.14

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <ctype.h>
  4 
  5 char *init_alp(char *temp)
  6 {
  7     int i;
  8     char alp = 'A';
  9 
 10     for (i = 0; i < 26; i++)
 11     {
 12         
 13         *(temp + i) = alp;
 14         alp++;
 15     }
 16     *(temp + i) = '\0';
 17 
 18     return temp;
 19 }
 20 
 21 int prepare_key( char *key )                        //生成key
 22 {
 23     int i = 0, j = 0, k = 0;
 24     char alp[27];
 25     char *temp = alp;
 26     
 27     char *p = NULL; 
 28 
 29     if (key == NULL)                                        //key為空,退出
 30         return 0;
 31 
 32     for (i = 0; *(key + i) != '\0'; i++)
 33         if (!(isalpha(*(key + i))))                    //key中含有非字母字符,退出)
 34             return 0;
 35 
 36     for (i = 0; *(key + i) != '\0'; i++)
 37     {
 38         if (*(key + i) != ' ')                                //刪除所有的空格
 39         {
 40             for (j = i + 1; *(key + j) != '\0'; j++)        //將重復的字母賦值為空格
 41             {
 42                 if ( *(key + i) == *(key + j) )
 43                     *(key + j) = ' ';
 44             }
 45             *(key + k) = toupper(*(key + i));
 46             k++;
 47         }        
 48     }
 49     *(key + k) = '\0';
 50 
 51     temp = init_alp(temp);                                //初始化為大寫字母表
 52     p = temp;
 53     while ((p = (strpbrk(p, key))) != NULL)                //將字母表中含有key中的字母設置為空格
 54     {
 55         *p = ' ';
 56         p++;
 57     }
 58 
 59     for (i = 0, k = 0; *(temp + i) != '\0'; i++)        //刪除所有的空格
 60     {
 61         if (*(temp + i) != ' ')                                
 62         {
 63             *(temp + k) = *(temp + i);
 64             k++;
 65         }
 66     }
 67     *(temp + k) = '\0';
 68 
 69     key = strcat(key, temp);
 70 
 71     return 1;
 72 }
 73 
 74 void encrypt( char *data, char const *key )            //給數據加密
 75 {
 76     int i, state;
 77     char temp[27];
 78     char *init = NULL;
 79     char *p = NULL;
 80     char chg;
 81 
 82     init = init_alp(temp);
 83     
 84     for (i = 0; *(data + i) != '\0'; i++)
 85     {
 86         if (isalpha(*(data + i)))
 87         {
 88             if (isupper(*(data + i)))
 89                 state = 1;
 90             else
 91             {
 92                 *(data + i) = toupper(*(data + i));
 93                 state = 0;
 94             }
 95             p = strchr(init, *(data + i));
 96             chg = *(key + (p - init));
 97             *(data + i) = state ? chg : tolower(chg);
 98         }
 99     }
100 }
101 
102 void decrypt( char *data, char const *key )            //給數據解密
103 {
104     int i, state;
105     char temp[27];
106     char *init = NULL;
107     char *p = NULL;
108     char chg;
109 
110     init = init_alp(temp);
111     
112     for (i = 0; *(data + i) != '\0'; i++)
113     {
114         if (isalpha(*(data + i)))
115         {
116             if (isupper(*(data + i)))
117                 state = 1;
118             else
119             {
120                 *(data + i) = toupper(*(data + i));
121                 state = 0;
122             }
123             p = strchr(key, *(data + i));
124             chg = *(init + (p - key));
125             *(data + i) = state ? chg : tolower(chg);
126         }
127     }
128 }
129 
130 int main (void)
131 {
132     char key[28] = "Hellomnnnnab";
133     char temp[28];
134     char *key_m = temp;
135     char data[] = "Hello world!";
136     
137     key_m = init_alp(key_m);
138     printf("%s\n", key);                //顯示輸入的key
139 
140     if (prepare_key(key))
141     {
142         printf("\nAlpha: \n");            //顯示大寫字母表
143         printf("%s\n", key_m);
144         printf("\nKey: \n");            //顯示生成的key秘鑰
145         printf("%s\n", key);
146 
147         printf("\nEncode: \n");
148         printf("%s\n", data);
149         encrypt(data, key);                //加密
150         printf("%s\n", data);            
151 
152         printf("\nDecode: \n");
153         printf("%s\n", data);
154         decrypt(data, key);                //解密
155         printf("%s\n", data);        
156     }
157     else
158         printf("Key error!\n");
159 
160 
161     getchar();
162     return 0;
163 }


9.15

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <ctype.h>
  4 
  5 void dollars(char *dest, char const *src)
  6 {
  7     int i, j, srclen = 0, offset = 0, state = 0;
  8     int a = 0, b = 0;
  9     char *destbak = NULL;
 10     destbak = dest;
 11 
 12         srclen = strlen(src);
 13         *dest++ = '$';
 14 
 15     if (srclen >= 3)
 16     {
 17         a = (srclen - 2) / 3;
 18         b = (srclen - 2) % 3;
 19 
 20         for (i = 0; (i < a); i++)
 21         {
 22             if ( i == 0 )
 23             {
 24                 for (j = 0; j < b; j++)
 25                 {
 26                     *(dest++) = *(src++);
 27                 }
 28             }
 29 
 30             if (b != 0)
 31                 *(dest++) = ',';
 32 
 33             for (j = 0; j < 3; j++)
 34                 *(dest++) = *(src++);
 35         }
 36 
 37         if ((a == 0) && (b != 0))
 38         {
 39             for (j = 0; j < b; j++)
 40             {
 41                 *(dest++) = *(src++);
 42             }
 43         }
 44         *((dest++)) = '.';
 45         
 46         for ( ; *(src) != '\0';dest++, src++)
 47             *(dest) = *(src);
 48         *(dest) = '\0';
 49     }
 50     else
 51     {
 52         memset(dest, '0', 4);
 53         *(++dest) = '.';
 54         dest++;
 55         for (i = 0; i < 2 - srclen; i++)
 56             *(dest++) = '0';
 57 
 58         for (i = 0; i < srclen; i++)
 59             *(dest + i) = *(src + i);
 60     }
 61 }
 62 
 63 int dollchk(char *src)                            //檢查要處理的數據中是否有非數字字符
 64 {
 65     int i;
 66     for (i = 0; *(src + i) != '\0'; i++)                
 67     {
 68         if (!isdigit(*(src + i)))
 69         {
 70             return 0;
 71         }
 72     }
 73     return 1;
 74 }
 75 
 76 int main (void)
 77 {
 78     char temp[20] = { 0 };                //目標緩存
 79     char dollar[20];
 80     char *dest = temp;
 81     int state = 0;
 82 
 83     printf("Please enter some numbers: \n");
 84     scanf("%s", dollar);
 85 
 86     state = dollchk(dollar);
 87     if (!state)                            //檢查要處理的數據中含有非數字字符,報錯,退出
 88     {
 89         printf("Data error!\n");
 90         getchar();
 91         getchar();
 92         return 0;
 93     }
 94 
 95     printf("%s\n", dest);
 96     printf("%s\n", dollar);
 97 
 98     dollars(dest, dollar);
 99     if (*dest != '\0')
100         printf("%s\n", dest);
101 
102     getchar();
103     getchar();
104     return 0;
105 }

 

 

9.16

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int format( char *format_string, char const *digit_string )
 5 {
 6     int formatlen = strlen(format_string);
 7     int digitlen = strlen(digit_string);
 8     int i, j, k;
 9     char *p = NULL;
10 
11     if (formatlen < digitlen)
12         return 0;
13     if ((*digit_string) == '\0')
14         return 0;
15 
16     if ((p = strchr(format_string, '.')) != NULL)
17     {
18         if ((k = formatlen - (p - format_string)) > digitlen)
19         {
20             for (i = formatlen, j = digitlen; j >= 0; i--, j--, k--)
21             {
22                 *(format_string + i) = *(digit_string + j);
23             }
24 
25             for (; k > 0; i--, k--)
26             {
27                 *(format_string + i) = '0';
28             }
29 
30             if ( (*(format_string + i) == '.') )
31             {
32                 i--;
33                 *(format_string + i) = '0';
34             }
35 
36             for (i--; i >= 0; i--)
37             {
38                 *(format_string + i) = ' ';
39             }
40         }
41         else
42         {
43             for (i = formatlen, j = digitlen; j >= 0; i--, j--)
44             {
45                 if ( (*(format_string + i) == ',') 
46                     || (*(format_string + i) == '.') )
47                     i--;
48                 *(format_string + i) = *(digit_string + j); 
49             }
50             for (; i >= 0; i--)
51                 *(format_string + i) = ' ';
52         }
53     }
54 
55     return 1;
56 }
57 
58 int main (void)
59 {
60     char formats[] = "###,###.#####";
61     char const digits[] = "123";
62 
63     printf("%s\n", formats);
64     printf("%s\n", digits);
65     if (format(formats, digits))
66         printf("%s\n", formats);
67     else
68         printf("Data error!\n");
69 
70     getchar();
71     return 0;
72 }

 

 

9.17

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved