《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
本站声明
本文转载自:http://www.cnblogs.com/microxiami/p/5028217.html     作者:micro虾米     发布日期:2015-12-08     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2016 ITdaan.com 粤ICP备14056181号