用指针将字符串数组位置颠倒

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void show(const char* const str);
 5 char* Position_swap(char* const str);
 6 
 7 int main(int argc, char* argv[])
 8 {
 9     char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
10     printf("Before the swap:");
11     show(str);
12     Position_swap(str);
13     printf("After  the swap:");
14     show(str);
15     return 0;
16 }
17 /**********************************************
18 *    头文件:#include <stdio.h>
19 *    功    能:打印字符数组信息
20 *    函    数:void show(char * str)
21 *    参    数:str字符数组首地址
22 *    返回值:无
23 **********************************************/
24 void show(const char* const str)
25 {
26     int len = strlen(str);
27     int i = 0;
28     while (i <= len)
29     {
30         printf("%c",*(str + i++));
31     }
32     printf("\n");
33 }
34 /**********************************************
35 *    头文件:#include <string.h>
36 *    功    能:实现字符数组位子互换
37 *    函    数:char* Position_swap(char * str)
38 *    参    数:str字符数组首地址
39 *    返回值:str字符数组首地址
40 **********************************************/
41 char* Position_swap( char * const str)
42 {
43     int len = strlen(str);
44     int i = 0, j = len-1;
45     char ch = 0;
46     while (i <= j)
47     {
48         ch = *(str + i); 
49         *(str + i) = *(str + j);
50         *(str + j) = ch;
51         i++;
52         j--;
53     }
54     return str;
55 }

 

posted @ 2021-01-16 17:46  王廷胡_白嫖帝  阅读(249)  评论(0)    收藏  举报