Hey guys, I'm having a bit of trouble with an assignment that has been racking my brain. I'm taking a number from the user, using a function to print its reverse order, checking to see if it is a palindrome in the main program and also removing any leading zero's from the results. I'm having no trouble printing the reverse order but the palindrome check and leading zero removal are driving my crazy. I tried using a loop to check each digit for palindromes but this doesn't seem very effective and returns a reply for each digit for a single statement. Here is the code:

#include<iostream>
using namespace std;

int reverse(int bef[], int &tot)
{
int i,j,t;

for(i=0, j=tot-1; i<j; i++, j--)
{t=bef; bef=bef[j]; bef[j]=t;}

}

int main()
{


int i;
int totdig;
cout << "Enter total number of digits in the number you would like reversed: ";
cin >> totdig;
int before[totdig];
int orig[totdig];
cout << "\nEnter the complete number, seperate each digit with a space: ";
for(i=0;i<totdig;i++) cin >> before;
for(i=0;i<totdig;i++) orig=before;
reverse(before, totdig);


for(i=0;i<totdig;i++) cout << orig;
cout << endl;
for(i=0;i<totdig;i++) cout << before;
cout << endl;

return 0;
}

For checking if the number is a palindrome you can use the two arrays you've already declared.

Just iterate down the list and compare them to see if they're equal. I would use a boolean statement.

bool same = false;
for (int i = 0; i < arraylength; i++)
{
        if (array1[i] == array2[i])
                    same = true;
        else
        {
                    same = false;
                    break;
         }
}

if (same == true)
     cout << "Number is palindrome";
else
     cout << "Number is not palindrome";

Assuming you have only one leading zero you could remove it by doing something like..

for (int i = 0; i < numlength; i++)
{
          if (i == 0 and array[0] == '0')
                      continue;
          else
                      array[i] = digit;
             
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.