1、使用指针实现 strcpy、strcat 函数的功能
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 char s1[21];
6 gets(s1);
7 char s2[21];
8 gets(s2);
9
10 char *p1=s1;
11 char *p2=s2;
12
13 while(*p2){
14 *p1=*p2;
15 p1++;
16 p2++;
17 }
18 *p1=*p2;
19 p1=s1;
20 puts(s1);
21
22
23
24 return 0;
25 }
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 char s1[21];
6 gets(s1);
7 char s2[21];
8 gets(s2);
9
10 char *p1=s1;
11 char *p2=s2;
12
13 while(*p1){
14 p1++;
15 }
16 while(*p2){
17 *p1=*p2;
18 p1++;
19 p2++;
20 }
21 *p1=*p2;
22 p1=s1;
23 puts(s1);
24
25
26 return 0;
27 }
2、设计一个程序,判断操作系统是大端存储还是小端存储
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 int a=0x12345678;
6 char *p=&a;
7 if(*p==0x12){
8 printf("大端\n");
9 }else if(*p==0x78){
10 printf("小端\n");
11 }
12
13 return 0;
14 }
3、指针实现冒泡排序
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 int s[5]={11,5,3,6,9};
6 int i=0,j=0,len,temp;
7 len=sizeof(s)/sizeof(int);
8
9 int *p1=s;
10
11 for(i=0;i<len-1;i++){
12 for(j=0;j<len-i-1;j++){
13 if(*(p1+j)>*(p1+j+1)){
14 temp=*(p1+j);
15 *(p1+j)=*(p1+j+1);
16 *(p1+j+1)=temp;
17 }
18 }
19 }
20 for(i=0;i<len;i++){
21 printf("%d ",s[i]);
22 }
23
24 return 0;
25 }
4、用指针的方式进行字符串倒叙。"abcdef" ==> "fedcba"
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 char s[21];
6 char temp=0;
7 gets(s);
8 char *p1=s;
9 char *p2=s;
10
11 while(*p1){
12 p1++;
13 }
14 p1--;
15 while(p1>p2){
16 temp=*p2;
17 *p2=*p1;
18 *p1=temp;
19 p1--;
20 p2++;
21 }
22 printf("%s\n",s);
23
24 return 0;
25 }
5、终端获取字符串删除空格,用指针方式实现
1 #include <stdio.h>
2
3 int main(int argc, const char *argv[])
4 {
5 char s[21];
6 gets(s);
7 char *p1=s;
8 char *p2=s;
9
10 while(*p1){
11 if(*p1==' '){
12 p1++;
13 }
14 if(*p1!=' '){
15 *p2=*p1;
16 p1++;
17 p2++;
18 }
19 }
20 *p2=*p1;
21 puts(s);
22 return 0;
23 }