[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

palindrome with recursion

Posted on 2004-10-27
6
Medium Priority
?
637 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
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

590 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