字符串反转

反转串

我们把“cba”称为“abc”的反转串。

下面的代码可以把buff中的字符反转。其中len表示buff中待反转的串的长度。

源代码:

 1 #include<stdio.h>
 2 #include <string.h>
 3 
 4 void reserve_string(char *buff, int len)
 5 {
 6     if(len < 2)return ;
 7 
 8     char temp = buff[0];
 9     buff[0] = buff[len - 1];
10     buff[len - 1] = temp;
11 
12     reserve_string(buff + 1, len - 2);
13 }
14 
15 int main()
16 {
17     const int STR_LEN = 1005;
18 
19     char str[STR_LEN];
20 
21     scanf("%s", str);
22 
23     reserve_string(str, strlen(str));
24 
25     printf("%s\n", str);
26 
27     return 0;
28 }
原文地址:https://www.cnblogs.com/Dreamcaihao/p/3055155.html