不使用标准函数反转字符串

时间:2012-08-09 16:43:32

标签: c++ string recursion reverse

我最近在接受采访时被问到这个问题。作为一名刚毕业的学生,​​只有2年的编程(所有的学校工作),我都不知所措。我有一个模糊的想法,但我确定我失败了。这就是我写的:

string Reverse(string word, string reversed)
{
    if(word.length() == 0)
    {
        return reversed;
    }
    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        Reverse(temp, reversed);
    }

    return reversed;
}

现在我回家了,我正在测试它,返回只是输入中的第一个字母。我模糊地熟悉递归的概念,但我显然在这方面失败了。任何帮助/指针/建议非常感谢。谢谢。

编辑: 在Dennis Meng的帖子之后,我做了以下改变:

string Reverse(string word, string reversed)
{
    if(word.length() == 0)
    {
        return reversed;
    }
    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        return Reverse(temp, reversed);
    }
}

现在,我得到了正确的返回值。非常感谢你。

6 个答案:

答案 0 :(得分:2)

这里出了什么问题:

    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        Reverse(temp, reversed); // <-- Here's your problem
    }

    return reversed;
}

你知道这个电话应该返回正确答案,为什么不回复呢?换句话说,如果你做了什么

    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        return Reverse(temp, reversed);
    }
}

代替?您的代码仅返回第一个字母的具体细节涉及传递引用/按值传递;因为传递值的东西,你从来没有真正使用过递归调用中所做的事情。 (你刚刚拨打电话,扔掉了它返回的内容。)

答案 1 :(得分:1)

我不确定你为什么在这里使用递归。这真的没必要:

string Reverse(string word)
{
    string reversed = "";

    if(word.length() == 0)
    {
        return reversed;
    } 

    for (int i = word.length()-1; i>=0; i--){
        reversed = reversed+word[i];
    }

    return reversed;
}

答案 2 :(得分:1)

string Reverse(string word, string reversed) 
{
    ...
    Reverse(temp, reversed);

首先,您应该通过const引用传递word,并通过引用传递reversed。当你调用递归函数时,你正在复制每个字符串,所以最外面的函数看不到它们做的任何事情。另一种选择是将递归函数的结果分配给reversed,但是到处都有一些愚蠢的字符串副本。所以:通过引用传递变量。

第二:有更简单的方法来反转字符串:

 string Reverse(string word) //caller should _not_ see my changes, so I pass by value
 {
     for(int i=0; i<word.size()/2; ++i) { //for each letter in the first half
         int otherindex = word.size()-1-i; //find the letter on the other half
         char t = word[i];  //and swap them
         word[i] = word[otherindex];
         word[otherindex] = t;
     }
     return word; //return the result
 }

答案 3 :(得分:0)

字符串只是一个字符数组。通常在这种情况下,您应该使用一个字符数组来解释算法。这是一种更好的方法。

void reverse(char* str, int len)
{
    for (int i = 0, j = len -1 ; i < len / 2; ++i, --j) {
         // Swap the values as i and j.
         int temp = str[i];
         str[i] = str[j];
         str[j] = temp;
    }
}

如果在Java中询问相同的问题,则必须确保将字符串转换为char []数组,然后将其转换为字符串。这有助于将创建的对象数量保持最小。至少,你应该使用StringBuilder。

答案 4 :(得分:0)

这是另一种解决方案(类似于已发布的解决方案)。 但是现在我看到这个解决方案对你来说不合适,因为它使用std东西。

std::string reverse(std::string const& character_queue_) {
    using namespace std;
    string result;
    if(!character_queue_.empty()) {
        vector<char> character_stack;
        std::copy(character_queue_.begin(),character_queue_.end(),std::back_inserter(character_stack));
        while(!character_stack.empty()) {
           result.push_back(character_stack.back());
           character_stack.pop_back();
        }
    }
    return result;
}

答案 5 :(得分:-2)

只是建议一种更好的处理递归的方法:

在C ++中使用递归进行字符串反转:

#include <iostream>
#include <string>
using namespace std;

string reverseStringRecursively(string str){
    if (str.length() == 1) {
        return str;
    }else{
        return reverseStringRecursively(str.substr(1,str.length())) + str.at(0);
    }
}

int main()
{
    string str;
    cout<<"Enter the string to reverse : ";
    cin>>str;

    cout<<"The reversed string is : "<<reverseStringRecursively(str);
    return 0;
}
相关问题