/* Problem To Compute the Next Palindrome,input is a palindrom or any number */


#include<iostream>
#include<string.h>
using std::cin;
using std::cout;
using std::endl;



unsigned long int MirrorDigit(int * Array,int Digit)
  {
      long int Temp=0;
      for(int i=0;i<Digit;++i)
          Temp=Temp*10+Array[i];
      return Temp;
  }

int main()
{
  unsigned long int Num;
  char NumStr[1000];

   cout<<"Enter the number: ";
   cin>>NumStr;

  /* Compute the Number of Digits & assighn the number into array */

  int DigitStr,Digit,Array[12];

  long int TempNum;  /* Variable to store the Input Number temporarily */

  DigitStr=strlen(NumStr);

  for(Digit=0,Num=0,TempNum=0;TempNum<DigitStr;TempNum++)
     {
         if (NumStr[TempNum]=='0')
           continue;
         Array[Digit]=NumStr[TempNum]-'0';
		 Num=Num*10+Array[Digit++];

     } /* At here the number is assigned into array */

//cout<<Digit;
/* Computing The desired mirrored number */

int Middle;  /*The Middle Digit */

int Start;   /* Middle-1;start of mirroring */
int Target;  /* Middle+1 */

/* Check whether the digits are odd or even */

if(Digit%2)  /*Number of Digits are odd */
  {
    Middle=Digit/2;
    Start=(Middle-1);
  }

else
  {
    Middle=(Digit/2)-1;
    Start=Middle;
  }

/* Arranging the degits in desired order */

for(Target=(Middle+1);Target!=Digit;Target++,Start--)  /*The Target will be same for both even & odd number of digits */
   {
       Array[Target]=Array[Start];
   }/* The desired Mirrored Image has been computed here */


long int Rev; /*Variable to store the reverse digit */

long int LoopCounter;

Rev=MirrorDigit(Array,Digit);

if(Rev<=Num)
  {
      /* Now we have to increment the middle number */

    for(Rev=0,LoopCounter=0;LoopCounter<Digit;++LoopCounter)
       {
           Rev=Rev*10+Array[LoopCounter];
           if(LoopCounter==Middle)
            Rev+=1;
       }



  /* Compute the Number of Digits & assighn the number into array */


  for(TempNum=Rev,Digit=0;TempNum != 0;Digit++)
     {
         Array[Digit]=TempNum%10;
         TempNum /= 10;
     } /* At here the number is assigned into array but in reverse order *

  /* Reversing the array Without any extra memory space i.e in the same memory location */

  int UB; /* Upper Boundary */
  int LB; /*Lower Boundary */

  int Counter; /*Counter*/

  Counter=(Digit)/2;
  UB=Digit-1;
  LB=0;

  for(int Temp;Counter != 0;UB--,LB++,Counter--)
     {
         Temp=Array[UB];
         Array[UB]=Array[LB];
         Array[LB]=Temp;
     }

 /* We again have to Arrange the digits */

  if(Digit%2)  /*Number of Digits are odd */
  {
    Middle=Digit/2;
    Start=(Middle-1);
  }

else
  {
    Middle=(Digit/2)-1;
    Start=Middle;
  }

/* Arranging the degits in desired order */

for(Target=(Middle+1);Target!=Digit;Target++,Start--)  /*The Target will be same for both even & odd number of digits */
   {
       Array[Target]=Array[Start];
   }/* The desired Mirrored Image has been computed here */

   Rev=MirrorDigit(Array,Digit);

  }

 cout<<"The Next Palindrome is:"<<Rev<<endl;

return 0;
}
