Creating a recursive function help.

So for one of my assignments, I have to call a recursive function that determines if the string is a palindrome.

*Examples of palindromes (for this program):
ABA
CCDDDCC
MADAMIMADAM
IVXLCDMDCLXVI

I am really bad at recursion. So can anyone make a recursive function like that for me and maybe explain it, if possible.

Thank you.
closed account (G30GNwbp)
This is a very common homework assignment; if you would just do a search engine query you would probably find multiple examples.
Hint: Do recursion on the length of the string with base case of 0. Each step, shorten the string by 2 chars. Just make sure you pick the right 2. :)
Last edited on
So a recursive function is a function that calls itself until a terminating condition is reached.

Say I want to calculate a Fibonacci number. The series is: 1, 1, 2, 3, 5, 8...
There's a pretty simple way to do this recursively

1
2
3
4
5
6
7
8
9
// Pretty shoddy version that requires current and prev to be 1 when first called,
// but it proves the point.
int fib(int current, int prev, int count)
{
    if ((count  == 0) || (count == 1))
        return current;
    else
        return fib(prev, (prev + current), (count - 1));
}


So all you really need to figure out are:
- What the conditions of exiting are
- What variables the function needs to pass to itself to keep recursng.

The rest should just be a few conditional statements
Topic archived. No new replies allowed.