4

I'm trying to program a recursive function that prints the reverse of the string, however, the compiler keeps saying it has too many arguments. What is wrong with this recursive program?

#include <iostream>
using namespace std;

void reverseDisplay (const string& s)
{

    int length=s.size()-1;
    if (length==0)
        return;

    reverseDisplay(s, length);
}

void reverseDisplay (const string& s, int n)
{
    if(n==1)
        cout <<s[n];

    else { 
        cout << s[n];
        reverseDisplay(s, n-1);
    }
}

int main()
{
    string s="12345";
    reverseDisplay(s);


    return 0;
}
1
  • And also, the if statement in reverseDisplay (const string& s, int n) should be if(n==0) Commented Oct 7, 2013 at 1:31

3 Answers 3

5

Try a forward declaration:

void reverseDisplay (const string& s, int n);

Put this before void reverseDisplay (const string& s).

Sign up to request clarification or add additional context in comments.

Comments

2

To solve a problem recursively, you want a function that calls itself, and a stopping condition. Using the size, however, is not necessary. Also, it will be more useful if you have a method that reverses a string; leave printing for later. Now, the typical way to reverse a string in any language is this bit of pseudocode:

reverse("abcd")            -->
        string suffix
reverse("abcd", ""       ) -->  
reverse("bcd",  "a" + "" ) ==
reverse("bcd",  "a"      ) --> string, suffix
reverse("cd",   "b" + "a") ==
reverse("cd",   "ba"     ) -->
reverse("d",    "c" + "ba")==
reverse("d",    "cba"    ) -->
reverse("", "d" + cba")
reverse("", "dcba")       --> Let this be the stopping condition.

So, we do this:

string reverse(const string original, string suffix);

string reverse(const string original) { 
    return reverse(original, new string);
} 

string reverse(const string original, string suffix) { 
    if (original.size()) {
         auto first = original.first();
         return reverse(original.substr(1), suffix.insert(0, 1, first));
    } else {
        return suffix;
    }
} 

I could probably have made some of these variables references.

Comments

2

You're not really doing recursion in reverseDisplay(const string& s). You're calling the other reverseDisplay(), which takes two arguments. This makes them overloaded, so the compiler will call the one that matches the arguments (or display an error if no such overload exists).

Here's a working recursive solution with just one display function:

#include <iostream>
#include <string>

void reverseDisplay(std::string const& s, std::size_t n)
{
    if (n == -1)
    {
        return;
    }

    std::cout << s[n];
    reverseDisplay(s, n-1);
}

int main()
{
    std::string s = "12345";
    std::size_t size = s.size();
    reverseDisplay(s, size-1);
}

3 Comments

Which should be before the other one, anyway.
Why use recursion at all for this? Why not use a simple for loop from length - 1 downto 0?
@KristerAndersson: I wasn't addressing that. In any event of recursion, the OP's code isn't doing that there. The reverseDisplay() with two arguments is doing this properly, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.