eg:Iuput:"hello"
Output:"olleh"
思想:设置两个字符指针,一个置于字符串最左端,一个置于字符串最右端,交换二者的内容后分别右移、左移,重复交换操作即可;(边界条件左侧、右侧指针相遇)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void tmp(char *a, char *b) {
char tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void reverse(char *str,char *pstep, char *ptmp) {
pstep = str;
ptmp = str;
while (*pstep!='\0') {//pstep向后遍历,找到字符串的结尾
pstep++;
}
pstep--; //pstep指向最后一个字母,ptmp指向第一个字母
while (ptmp < pstep) {//ptmp从左向右,pstep从右向左,不断执行交换函数,完成整个字符串的逆转,直到ptmp>=pstep为止
tmp(ptmp, pstep);
ptmp++;
pstep--;
}
}