实验五

实验一:

(一)

1.找到数组中最大值和最小值。

2.pmin指向变量min,pmax指向变量max。

(二)

1.找到数组中最大值的地址,返回的是地址。

2.可以。

实验二:

(一)

1.数组s1大小是80;sizeof计算的是数组s1占用的字节数;strlen计算的是字符串长度。

2.不能,s1是常量指针,不能作为左值被赋值。

3.交换了。

(二)

1.存放的是"Learning makes me happy"的地址;sizeof计算的是指针变量的大小;strlen计算的是字符串的长度。

2.可以。

3.交换的是s1和s2指针变量的值;没有。

实验三:

已完成

实验四:
1.将字符串中的 " i " 替换成 " * "。

2.可以。

实验五:

会在第二次时不做任何改变;作用是存放空格;

 1 char *str_trunc(char *str, char x) {
 2     char * p = str;
 3     int i = 0;
 4     while (*p)
 5     {
 6         if (*p == x)
 7             *p = '\0';
 8         p++;
 9     }
10 
11 }
View Code

实验六:

 1 int check_id(char *str) {
 2     int i = 1;
 3     char* p = str;
 4     if (strlen(str) != 18) return 0;
 5     while (*p && i <= 17) {
 6         if (*p < '0' || *p>'9') return 0;
 7         p++;
 8         i++;
 9     }
10     if (*p == 'X' || *p >= '0' && *p <= '9') return 1;
11     else return 0;
12 }
View Code

实验七:

 1 void encoder(char *str, int n) {
 2     char* p = str;
 3     while (*p) {
 4         if (*p >= 'a' && *p <= 'z') {
 5             *p += n;
 6             if (*p > 'z')*p -= 26;
 7         }
 8         if (*p >= 'A' && *p <= 'Z') {
 9             *p += n;
10             if (*p > 'Z')*p -= 26;
11         }
12         p++;
13     }
14 }
15 
16 void decoder(char* str, int n) {
17     char* p = str;
18     while (*p) {
19         if (*p >= 'a' && *p <= 'z') {
20             *p -= n;
21             if (*p < 'a')*p += 26;
22         }
23         if (*p >= 'A' && *p <= 'Z') {
24             *p -= n;
25             if (*p < 'A')*p += 26;
26         }
27         p++;
28     }
29 }
View Code

实验八:

 1 #include <stdio.h>
 2 #include<string.h>
 3 
 4 void sort(char* ps[], int t);
 5 
 6 int main(int argc, char *argv[]) {
 7     int i;
 8     sort(argv, argc);
 9 
10     for(i = 1; i < argc; ++i)
11         printf("hello, %s\n", argv[i]);
12 
13     return 0;
14 }
15 
16 void sort(char* ps[], int t) {
17     char* p;
18     int j, k;
19     for (j = 0; j < t-1; j++)
20         for (k = 1; k < t - j - 1; k++)
21             if (strcmp(ps[k], ps[k + 1])>0) {
22                 p = ps[k];
23                 ps[k] = ps[k + 1];
24                 ps[k + 1] = p;
25             }
26 }
View Code

 

posted @ 2026-05-31 20:29  郑云翔  阅读(12)  评论(0)    收藏  举报