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:

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;

Edited by dmitriylm: n/a

7 Years
Discussion Span
Last Post by Kontained

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;
                    same = false;

if (same == true)
     cout << "Number is palindrome";
     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')
                      array[i] = digit;

Edited by Kontained: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.