C++课后练习,编写函数void reverse(string &s),用递归算法使字符串s倒序

#include <iostream>

using namespace std;

void reverse(string &s)
{
    static string s2;
    int Size1=s.length(),Size2=s2.length();
    if(Size1==Size2)
    {
        s=s2;
        return;//因为函数返回值类型为空,所以什么都不返回,这里只代表递归调用结束。
    }
    s2+=s.at(Size1-Size2-1);
    reverse(s);
}
int main()
{
    //cout << "Hello world!" << endl;
    string s="abcdef";
    reverse(s);
    cout<<s;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coco56/article/details/80304848
今日推荐