0

为了探索我对递归的理解,我试图通过使用递归函数来反转字符串。这似乎应该比现在对我来说更简单。谁能告诉我我做错了什么。当我执行下面的代码时,它会产生一个空行。我在这里四处寻找类似的主题,但每件事都是用其他语言写的……令我惊讶的是。

#include <iostream>
#include <string>

using namespace std;


/**
    Recursivly reverses a string
    @param return last_char, the last character currently in the string
    @param go, the recursive function to return the character and continue     within the function
    **/
char string_reverse(string word)
{

    if (word.length()-1 > 0)
    {
    char last_char = word[word.length()-1];
    word.erase(word.length()-1);
    char go = string_reverse(word);
    return go;

    }

else 
    return false;

}


int main()
{
cout << "Enter a string: ";
string input;
getline(cin, input);
string last;
last = last + string_reverse(input);
cout << last << endl;

/*char fig = string_reverse(input, fig);
cout << fig << endl;
*/

system("pause");
return 0;
}
4

7 回答 7

3

在中string_reverse,您应该返回last character++ string_reverse(word)first character

在您的else中,返回一个空字符串,这样您就不会遇到输入错误。

当您调用该函数时,不要对 做任何其他事情word,只需调用string_reverse(word)

把它们放在一起:

#include <iostream>
#include <string>

using namespace std;


/**
    Recursivly reverses a string
    @param return last_char, the last character currently in the string
    @param go, the recursive function to return the character and continue
    within the function
    **/
string string_reverse(string word)
{

    if (word.length()-1 > 0)
    {
    string first_char = word.substr(0,1);
    string last_char = word.substr(word.size()-1,1);
    string middle = word.substr(1, word.size()-2);
    return last_char + string_reverse(middle) + first_char;

    }

else 
    return "";

}


int main()
{
cout << "Enter a string: ";
string input;
getline(cin, input);
cout << string_reverse(input); << endl;

system("pause");
return 0;
}

但是,对于奇数字母计数,这将失败。“c”将输出“cc”。我会把它留给你。

于 2013-03-29T13:55:37.233 回答
2

string_reverse()返回char。您不可能使用单个char.

最重要的是,其中的逻辑距离做任何接近反转字符串的事情string_reverse()还有很长的路要走。

于 2013-03-29T13:55:14.170 回答
2

您需要返回字符串,还需要将提取的字符添加到返回值

string string_reverse(string word)
{
    if (word.length() - 1 > 0)
    {
        char last_char = word[word.length()-1];
        word.erase(word.length()-1);
        string go = string_reverse(word);
        return go.insert(0, 1, last_char);
    }
    else
        return "";
}
于 2013-03-29T13:59:27.633 回答
0

你的反向身体根本不起作用。反转的思路如下:

If the string len is 1, return it.
Otherwise,
   Remove the first character.
   Recursively reverse the remaining string.
   Add the first character above to the reversed string.
   Return the new string.
于 2013-03-29T14:13:53.410 回答
0

如果您返回字符串,您可以获得的最短版本将是:

std::string string_reverse(const std::string& str)
{
  return str.size() > 1? string_reverse(str.substr(1)) + str[0] : str;
}
于 2014-02-09T18:13:19.407 回答
0

这是对上述答案之一的小调整,可处理偶数或奇数长度的字符串:

string reverse (string str)
{

if (str.length() == 0)
    return "";
else
    if (str.length() == 1)
        return str.substr(0,1);
    else
    {
        string first_char = str.substr(0,1);
        string last_char = str.substr(str.size()-1,1);
        string middle = str.substr(1, str.size()-2);
        return last_char + reverse(middle) + first_char;
    }
 }

长度为 0 的字符串和长度为 1 的字符串必须分别与递归情况分开处理。

于 2015-04-08T20:42:34.767 回答
0

Found an easy way to rewrite code without all the function calls that works for odd or even number strings. Seems more efficient please note if there is any bugs, this was a homework question I solved. Would love any comments to correct issues

string R_reverse(string input)
{
	if (input.length() == 0)
		return input;
	else
	{
		string old = input.substr(0,1);
		string newstr = input.substr(1, (input.length() - 1));
		return R_reverse(newstr) + old;
	}
}

于 2016-12-04T05:16:04.123 回答