Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

palindrome with recursion

Posted on 2004-10-27
6
Medium Priority
?
635 Views
Last Modified: 2010-04-01
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;
            }
}
0
Comment
Question by:jandhb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 12430307
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12430783
>> 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
 
LVL 12

Expert Comment

by:stefan73
ID: 12431545
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jandhb
ID: 12432523
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 100 total points
ID: 12433115
>>>> 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
 
LVL 1

Author Comment

by:jandhb
ID: 12441062
Alex, thank you very much for your help here.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question