#include <stdio.h> void reverse_string(char * string) { int count = 0; char *p = string; char temp; while(*p != '\0') { count++; //计算字符串长度 p++; } if(count > 1) { temp = string[0]; //将最后一个字符赋给第一个字符,第一个字符保存,最后一个字符赋为空 string[0] = string[count-1]; string[count-1] = '\0'; reverse_string(string+1); //进行函数递归 string[count-1] = temp; } } int main() { char string[10] = "abcdef"; reverse_string(string); printf("%s\n",string); return 0; }
<img src="http://img.blog.csdn.net/20150414154319360?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYnl6MTk5Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
编写一个函数reverse_string(char * string)(递归实现)
原文地址:http://blog.csdn.net/byz1993/article/details/45042959