# palindrome with recursion

I am trying to write a palindrome function  that will tell me if a number is a palindrome. The tricky part about this, I think, is that I want to call the following two recursive functions from within the palindrome() to do the work.

-----------------------
unsigned digitCounter(int number)
{
if (number != 0)
{
return digitCounter(number / 10) + 1;
}
else
{
return 0;
}
}
---------------------
int power(int x, unsigned n)
{
if (n == 0)
{
//anchor case
return 1;
}
else
{      //inductive case (n > 0)
return x * power(x, n - 1);
}
}
---------------------
Here is what I have for the palindrome() so far, but its not quite right.

bool palindrome(int number)
{
int firstDigit, lastDigit;
if (number == 1)
{
return true;
}
else

firstDigit = number / (10 ^ numDigits - 1);
//gives last digit
lastDigit = number % 10;

if (firstDigit != lastDigit)
{
return false;
}
else
{
palindrome(number % 10) / 10 && lastDigit - 2;
}
}
LVL 1
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
bool palindrome(int number)
{
int firstDigit, lastDigit;
/* calculate numDigits here. the function looks correct */

if (numDigits == 1)
{
return true;
}
else

firstDigit = number / (10 ^ numDigits - 1);
lastDigit = number % 10;

if (firstDigit != lastDigit)
{
return false;
}
else
{
int newNumber = number - firstDigit*(10 ^ numDigits - 1); //remove first digit
newNumber /= 10; //remove last digit
palindrome(newNumber);
}
}
0
Commented:
>> I want to call the following two recursive functions from within the palindrome()

But you didn't...

bool palindrome(int number)
{
int firstDigit, lastDigit;

int numDigits = digitCounter(number);

if (numDigits == 1)
{
return true;
}
else
{
int dec = 10;
for (int i = 0; i < numDigits-1; ++i)
dec *= 10;
firstDigit = number / dec;
lastDigit = number % 10;

if (firstDigit != lastDigit)
{
return false;
}
else
{
int newNumber = number - dec; //remove first digit
newNumber /= 10; //remove last digit
return palindrome(newNumber);
}
}
}

Regards, Alex
0
Commented:
Hi jandhb,
Do you need to process your number as an integer? It's a lot easier when you convert it into text first:

bool palindrome(int number){
char ptext[12]; /* 10 digits, sign and trailing \0 */

sprintf(ptext,"%d",number);

return is_palindrome(ptext,0,strlen(ptext)-1);
}

...I'm sure you can easily write the is_palindrome() function yourself.

Cheers!

Stefan
0
Author Commented:
Do you need to process your number as an integer?

The number I will be passing to this function will be an integer. In your examples where are you calling the two other functions from within the palindrome()? These recursive functions need to be called to do the work. Maybe I'm missing it. Here again are the two other functions....

unsigned digitCounter(int number)
{
if (number != 0)
{
return digitCounter(number / 10) + 1;
}
else
{
return 0;
}
}
----------------------------------------------
int power(int x, unsigned n)
{
if (n == 0)
{
//anchor case
return 1;
}
else
{      //inductive case (n > 0)
return x * power(x, n - 1);
}
}
----------------------------------------

0
Commented:
>>>> Maybe I'm missing it.

Yes, i used digitCounter in the post above. I didn't use power(), but made a while loop calculating the power of 10 myself. I removed some errors and used both  functions now:

unsigned digitCounter(int number)
{
if (number != 0)
{
return digitCounter(number / 10) + 1;
}
else
{
return 0;
}
}

int power(int x, unsigned n)
{
if (n == 0)
{
//anchor case
return 1;
}
else
{     //inductive case (n > 0)
return x * power(x, n - 1);
}
}

bool palindrome(int number)
{
int firstDigit, lastDigit;

int numDigits = digitCounter(number);

if (numDigits <= 1)
{
return true;
}
else
{
int dec = power(10, numDigits-1);
firstDigit = number / dec;
lastDigit = number % 10;

if (firstDigit != lastDigit)
{
return false;
}
else
{
int newNumber = number - (firstDigit*dec); //remove first digit
newNumber /= 10; //remove last digit
return palindrome(newNumber);
}
}
}

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Alex, thank you very much for your help here.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.