65,209
社区成员
发帖
与我相关
我的任务
分享#include<string>
#include<iostream>
using namespace std;
void Reverse(string &s,int i,int j){
if(i>=j)return;
char c = s[i]; s[i] = s[j]; s[j]=c;
Reverse(++i,--j);
}
void Reverse(string &s){ Reverse(0,s.size()-1); }
int main()
{
string s("abcdefg");
cout<<"Before reverse: s = "<<s<<endl;
Reverse(s);
cout<<"After reverse: s = "<<s<<endl;
}
#include <string>
#include <iostream>
using namespace std;
void Reverse(string &s)
{
size_t len = s.length();
static int index = 0;
if (index <= (len/2))
{
char temp = s[index];
s[index] = s[len-index-1];
s[len-index-1] = temp;
++index;
Reverse(s);
}
}
int main(int argc, const char * argv[])
{
string s = "abcdefg";
Reverse(s);
cout << s << endl;
return 0;
}
#include <string>
#include <iostream>
using namespace std;
void Reverse(string &s)
{
static string copy_str = string(s.length(), '\0');
static int index;
if (index == s.length())
{
s = string(copy_str);
return;
}
copy_str[index] = s[s.length()-index-1];
index++;
Reverse(s);
}
int main(int argc, const char * argv[])
{
string s = "abcdefg";
Reverse(s);
cout << s << endl;
return 0;
}
#include <string>
#include <iostream>
using namespace std;
void Reverse(string &s, int index)
{
static string copy_str = string(s.length(), '\0');
if (index == s.length())
{
cout << copy_str << endl;
return;
}
copy_str[index] = s[s.length()-index-1];
Reverse(s, index+1);
}
int main(void)
{
string s = "abc";
Reverse(s, 0);
return 0;
}