# C++ algorithm help

• 10-08-2002
Gene126
C++ algorithm help
I am taking a programming class and school and I am trying to write a program. I have tried some stuff but I cant figure out how to do it. What the program does is take an unsigned integer as input calculates if it is a palindrome or not, a number that is the same forwards as it is backwards. I dont want to program written for me I just wont some help on how I would go about this.

I must write to functions. One that tells me if the integer is a palindrome or not and another that will tell me the digit place in a integer. like if the int was 5589 and I wanted the second place it would return 8.

Thanks
• 10-08-2002
anobody
try taking the number in as a string. then just flip it and compair it to the original
• 10-08-2002
XSquared
If you're looking for speed, try this:
Code:

```bool checkPalindrome(int testSubject) {         int tempValue = 0;         int origValue = testSubject;         int pValue;         while (testSubject != 0)         {                 pValue = testSubject;                 pValue %= 10;                 tempValue *= 10;                 tempValue += pValue;                 testSubject /= 10;         }         if(origValue == tempValue)         {                 return true;         } else {                 return false;         } }```
• 10-09-2002
Prelude
There are a number of ways to go about this. Here is my solution this time, the value is reversed into a std::string and tested for a palindrome. The string is reversed because you wanted the indexing function to work from the least signifigant digit to the most signifigant. This way was easier.
Code:

```#include <iostream> #include <string> class Palin {   int value; // Original value   std::string buffer; // Reversed value   // Check the value for a palindrome   bool check_val(); public:   Palin ( int val )     : value ( val )   {}   // Print the results of check_val()   void check ( std::ostream& out );   // Get the digit of value located at index   // in the least signifigant order.   //  Ex. 1234, get_digit ( 2 ) returns 3.   int get_digit ( const int index ); }; bool Palin::check_val() {   int low, high, count = 0;   int copy = value;   // Copy the integer to the buffer in reverse   for ( count = 0; copy != 0; count++ ) {     buffer += ( copy % 10 ) + '0';     copy /= 10;   }   // Check for a palindrome   for ( low = 0, high = count - 1; low < high; low++, high-- ) {     if ( buffer[low] != buffer[high] )       return false;   }   return true; } void Palin::check ( std::ostream& out) {   if ( check_val() )     out<< value <<" is a palindrome.\n";   else     out<< value <<" is a not palindrome.\n"; } int Palin::get_digit ( const int index ) {   return buffer[index - 1] - '0'; } int main() {   Palin my_pal ( 1221 );   my_pal.check ( std::cout );   std::cout<<"The second digit is "<< my_pal.get_digit ( 2 ) <<'\n';   return 0; }```
-Prelude
• 10-10-2002
Gene126
Howdo I flip the string around?
how do I flip a string around?

Thanks
• 10-10-2002
Prelude
>how do I flip a string around?
You can either copy it to another buffer in reverse, or swap elements from the outside in until you get to the middle:
Code:

```copy to a buffer: step 1: 1234 5 step 2: 123  54 step 3: 12  543 step 4: 1    5432 step 5:      54321 swap elements: step 1: 12345 step 2: 52341 step 3: 54321```
-Prelude
• 10-10-2002
ripper079

Quote:

how do I flip a string around?

You could iterate backwards

Code:

```#include <iostream> #include <string> using namespace std; int main() {   string name;   string reverse;     cout << "Enter a name: ";   cin >> name;     for (int i = name.size() -1; i > -1; i--)       reverse += name[i];         cout << "Reverse name: " << reverse << endl;   return 0; }```
• 10-10-2002