2

I'm writing a program that checks if a number is a palindrome using recursion. I used a static variable to keep the value of the number written backwards and at the end I compared if it was equal to the original number.

It works fine when testing one number but if I try to check more than one number (I ask the user to input a few numbers in a for loop in the main), it checks the first number just fine but the others it doesn't.

Is there a way to reset the static variable back to 0 after the recursion ends (after checking one number)?

bool IsPalindrome(unsigned int number) {
    static unsigned int Inverse = 0;
    unsigned int OriginalLastDigit, OriginalNumber=number;
    if (number != 0) {
        OriginalLastDigit = number % 10;
        Inverse = Inverse * 10 + OriginalLastDigit;
        IsPalindrome(number / 10);
    }
    if (OriginalNumber == Inverse) {
        return true;
    } else {
        return false;
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Titan3
  • 106
  • 10
  • 4
    The answer is simple - using static variables in recursion is "cheating". Pass it as additional parameter instead, so your function is becoming "pure". – Eugene Sh. Jun 21 '18 at 16:39
  • By the way, you don't use the return value of the recursive call. – Eugene Sh. Jun 21 '18 at 16:41
  • 1
    I would suggest a different approach - write a recursive function "ReverseNumber", and then a wrapper comparing `n == ReverseNumber(n)`. This logic will be much cleaner. – Eugene Sh. Jun 21 '18 at 16:55

3 Answers3

4

Structuring it as

bool IsPalindrome(unsigned int number) {
  unsigned int Inverse = 0;
  return IsPalindromeHelper(number, &Inverse);
}

with your original code slightly changed so the signature is

bool IsPalindromeHelper(unsigned int number, unsigned int* Inverse)

Is probably the easiest way to accomplish what you want to do. Knowing when to reset a static variable isn't feasible, short of passing in a flag... but if you're passing in a flag it's much cleaner to just pass in the variable itself as others have noted.

patros
  • 7,719
  • 3
  • 28
  • 37
  • You forgot to change the definition. – Eugene Sh. Jun 21 '18 at 16:49
  • Also I am not sure... what does it achieve? What is the use of `Inverse` here? Update: Ah, ok, got it. It's two functions... – Eugene Sh. Jun 21 '18 at 16:50
  • The original code seems to be ignoring the recursive call values, and relying entirely on Inverse to handle recursive state. I haven't bothered to unwind the actual logic of what this code does, but this is the way to replicate the same behavior without using a static variable. – patros Jun 21 '18 at 16:53
  • Yes that worked, although it did take me a bit of trial and error to get the code to work since I am new to pointers.one of the mistakes that I made when I changed the code was that I kept calling for the original function in the new helper function – Titan3 Jun 21 '18 at 18:01
0

You can reset it when number == 0 but I wouldn't advise using a static variable as it causes issues such as this one, it isn't thread safe, etc.

An alternative is to pass it as a parameter.

f4.
  • 3,814
  • 1
  • 23
  • 30
-1
bool IsPalindrome(unsigned int number, unsigned int Inverse)
{
    unsigned int OriginalLastDigit, OriginalNumber = number;
    if (number != 0)
    {
        OriginalLastDigit = number % 10;
        Inverse = Inverse * 10 + OriginalLastDigit;
        IsPalindrome(number / 10, Inverse);
    }
    if (OriginalNumber == Inverse)
    {
        return true;
    }
    else
    {
        return false;
    }
}
Som
  • 185
  • 15