编写一个函数reverse_string(char * string)(递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。
#include<stdio.h> #include<stdlib.h> #include<assert.h> int my_strlen(const char *str)//const所修饰的不会被修改 { assert(str);//断言 int count = 0; while (*str) { count++; str++; } return count; } void reverse_string(char * s) { char *p = s; int len = my_strlen(p); if (len > 0) { char tmp = p[0]; p[0] = p[len - 1]; p[len - 1] = ‘\0‘; reverse_string(p + 1);//递归 p[len - 1] = tmp; } } int main() { char str[] = "abcdef"; reverse_string(str); printf("%s",str); system("pause"); return 0; }
本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1716927
原文:http://10797127.blog.51cto.com/10787127/1716927