再帰についての理解を深めるために、再帰関数を使用して文字列を逆にしようとしています。これは、今の私にとってよりも単純なはずです。私が間違っていることを誰かに教えてもらえますか。以下のコードを実行すると、空白行になります。私はここで同様のトピックを探しましたが、すべてが他の言語で書かれています...驚いたことに.
#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;
}