思路: 创建一个指向数组头的指针和数组尾的指针,然后首尾位置上的变量逐个交换值,最后到中间即2/len就完成了。
(注意:三次异或交换两个数时,被交换的两个数不能相等不然会出错。)
#include <stdio.h>#include <String.h>#define _CRT_SECURE_NO_WARNINGS#define _CRT_SECURE_NO_DEPRECATE#pragma warning(disable:4996);char* Reverse(char* buf){printf("Please input string:");scanf("%s", buf);int len = strlen(buf);char* tmp = buf+ len-1;char* head = buf;for (size_t i = 0; i < len / 2; i++){if(*buf!=*tmp){*buf ^= *tmp;*tmp ^= *buf;*buf ^= *tmp;}buf++;tmp--;}return head;}int main(){char buf[100] = "";printf("%s", Reverse(buf));}