郑莉cpp习题6-22 用递归算法翻转字符串s
郑莉cpp习题6-22 用递归算法翻转字符串s
#include <iostream>
using namespace std;
#include <string>
void reverse(string &s,int left,int right){
char t;
if(left < right){
t = s[left];
s[left] = s[right];
s[right] = t;
reverse(s,++left,--right);
}
}
void reverse(string &s){
int size = s.size();
reverse(s,0,size-1);
}
int main(){
string s1 = "abcdefghijk";
reverse(s1);
cout << s1 << endl;
return 0;
}
结果
kjihgfedcba