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