task1_1.c
1 #include<stdio.h>
2 #define N 4
3
4 int main()
5 {
6 int x[N]={1,9,8,4};
7 int i;
8 int *p;
9
10 p=x;
11 for(i=0;i<N;++i)
12 printf("%d",*(p+i));
13 printf("/n");
14
15 return 0;
16 }

task1_2.c
1 #include<stdio.h>
2 #define N 4
3
4 int main()
5 {
6 char x[N]={'1','9','4','8'};
7 int i;
8 char *p;
9
10 p=x;
11 for(i=0;i<N;++i)
12 printf("%c",*(p+i));
13 printf("/n");
14
15 return 0;
16 }

1.2004
2.2001
3.task1_1指向整型变量,占用4四字节;task1_2指向字符型变量,占用1字节。
task2_1.c
1 #include<stdio.h>
2
3 int main()
4 {
5 int x[2][4]={{1,9,8,4},{2,0,2,1}};
6 int i,j;
7 int *p;
8 int(*q)[4];
9
10 for(i=0;i<2;++i)
11 {
12 for(j=0;j<4;++j)
13 printf("%d",x[i][j]);
14 printf("/n");
15 }
16
17 for(p=&x[0][0],i=0;p<&x[0][0]+8;++p,i++)
18 {
19 printf("%d",*p);
20 if((i+1)%4==0)
21 printf("/n");
22 }
23
24 for(q=x;q<x+2;++q)
25 {
26 for(j=0;j<4;++j)
27 printf("%d",*(*q+j));
28 printf("/n");
29 }
30
31 return 0;
32 }

task2_2.c
1 #include<stdio.h>
2
3 int main()
4 {
5 char x[2][4]={{'1','9','8','4'},{'2','0','2','1'}};
6 int i,j;
7 char *p;
8 char (*q)[4];
9
10 for(i=0;i<2;++i)
11 {
12 for(j=0;j<4;++j)
13 printf("%c",x[i][j]);
14 printf("/n");
15 }
16
17 for(p=&x[0][0],i=0;p<&x[0][0]+8;++p,i++)
18 {
19 printf("%c",*p);
20 if((i+1)%4==0)
21 printf("/n");
22 }
23
24 for(q=x;q<x+2;++q)
25 {
26 for(j=0;j<4;++j)
27 printf("%c",*(*q+j));
28 printf("/n");
29 }
30
31 return 0;
32 }

1.2004 2016
2.2001 2004
3.p指向元素的地址,q指向行地址
task3_1.c
1 #include<stdio.h>
2 #include<string.h>
3 #define N 80
4
5 int main()
6 {
7 char s1[]="C,I love u.";
8 char s2[]="C,I hate u.";
9 char tmp[N];
10
11 printf("sizeof(s1) vs. strlen(s1):/n");
12 printf("sizeof(s1)=%d/n",sizeof(s1));
13 printf("strlen(s1)=%d/n",strlen(s1));
14
15 printf("/nbefore swap:/n");
16 printf("s1: %s/n",s1);
17 printf("s2: %s/n",s2);
18
19 printf("/nswapping.../n");
20 strcpy(tmp,s1);
21 strcpy(s1,s2);
22 strcpy(s2,tmp);
23
24 printf(" /nafter swap: /n");
25 printf("s1: %s/n",s1);
26 printf("s2: %s/n",s2);
27
28 return 0;
29 }

1.s1的大小为13字节,sizeof(s1)计算s1的储存空间,strlen(s1)统计字节长度
2.不可以,定义错误
3.是
task3_2.c
1 #include<stdio.h>
2 #include<string.h>
3 #define N 80
4
5 int main()
6 {
7 char *s1="C,I love u.";
8 char *s2="C,I hate u.";
9 char *tmp;
10
11 printf("sizeof(s1) vs. strlen(s1):/n");
12 printf("sizeof(s1)=%d/n",sizeof(s1));
13 printf("strlen(s1)=%d/n",strlen(s1));
14
15 printf("/nbefore swap:/n");
16 printf("s1: %s/n",s1);
17 printf("s2: %s/n",s2);
18
19 printf("/nswapping.../n");
20 tmp=s1;
21 s1=s2;
22 s2=tmp;
23
24 printf(" /nafter swap: /n");
25 printf("s1: %s/n",s1);
26 printf("s2: %s/n",s2);
27
28 return 0;
29 }

1.s1存放字符串的地址,sizeof(s1)计算字符串地址占用的储存空间,strlen(s1)统计字符串长度
2.不能,s1是地址,不能赋值
3.交换的是指针地址,存储单元没有交换
task4.c
1 #include<stdio.h>
2 #include<string.h>
3 #define N 5
4
5 int check_id(char *str);
6
7 int main()
8 {
9 char *pid[N]={"31010120000721656X","330106199609203301",
10 "53010220051126571","510104199211197977",
11 "53010220051126133Y"};
12 int i;
13
14 for(i=0;i<N;++i)
15 if(check_id(pid[i]))
16 printf("%s/tTrue/n",pid[i]);
17 else
18 printf("%s/tFalse/n",pid[i]);
19
20 return 0;
21 }
22
23 int check_id(char *str)
24 {
25 int i,j;char temp[20];
26 for(i=0;i<N;i++)
27 {
28 if(strlen(str)!=18)
29 return 0;
30
31 strcpy(temp,str);
32 for(j=0;j<strlen(str)-1;j++)
33 {
34 switch(temp[j]>'0'&&temp[j]<'9')
35 {
36 case 1:if((temp[17]>'0'&&temp[17]<'9')||temp[17]=='X')
37 {
38 return 1;
39 }
40 else
41 {
42 return 0;
43 }
44 case 0:return 0;
45 }
46 }
47 }
48 }

task5.c
1 #include<stdio.h>
2 #include<string.h>
3
4 #define N 80
5 int is_palindrome(char *s);
6
7 int main()
8 {
9 char str[N];
10 int flag;
11
12 printf("Enter a string:/n");
13 gets(str);
14
15 flag=is_palindrome(str);
16
17 if(flag)
18 printf("YES/n");
19 else
20 printf("NO/n");
21
22 return 0;
23 }
24
25 int is_palindrome(char *s)
26 {
27 int i;
28 for(i=0;i<strlen(s)/2;i++)
29 {
30 if(s[i]!=s[strlen(s)-1-i])
31 {
32 return 0;
33 break;
34 }
35 }
36 return 1;
37 }


task6.c
1 #include<stdio.h>
2 #define N 80
3
4 void encoder(char *s);
5 void decoder(char *s);
6
7 int main()
8 {
9 char words[N];
10 printf("输入英文文本:");
11 gets(words);
12
13 printf("编码后的英文文本:");
14 encoder(words);
15 printf("%s/n",words);
16
17 printf("对编码后的英文文本解码:");
18 decoder(words);
19 printf("%s/n",words);
20
21 return 0;
22 }
23
24 void encoder(char *s)
25 {
26 int i;
27 for(i=0;s[i]!='/0';i++)
28 {
29 if((s[i]>='a'&&s[i]<'z')||(s[i]>='A'&&s[i]<'Z'))
30 {
31 s[i]+=1;
32 }
33 else if(s[i]=='z')
34 {
35 s[i]='a';
36 }
37 else if(s[i]=='Z')
38 {
39 s[i]='A';
40 }
41 }
42 }
43
44 void decoder(char *s)
45 {
46 int i;
47 for(i=0;s[i]!='/0';i++)
48 {
49 if((s[i]>'a'&&s[i]<='z')||(s[i]>'A'&&s[i]<='Z'))
50 {
51 s[i]=s[i]-1;
52 }
53 else if(s[i]=='a')
54 {
55 s[i]='z';
56 }
57 else if(s[i]=='A')
58 {
59 s[i]='Z';
60 }
61 }
62 }

原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/267194.html