Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Oct 9, 2015

Palindrome Generation

This is a code is written with the gcc platform (Ubuntu, LINUX); however, since it is C-based code, it should be portable. The listing generates a palindrome number from some given numbers. The operation is based on stack and will actually generate the palindrome of length ( 2n -1 ) for a given n numbers, so if n is 3, then this would be (23-1), which is 8 - 1, and thus 7. This is the special property.

I hope, this may help the beginners.

The code:


/*	This is a Process of generating Palindrome number from some given numbers	*/
#include <stdio.h>
#define max 10
#define maxim 30

int pushA (int[], int*, int*);
int cpyAC (int[], int[], int*);
int palgen (int[], int[], int[], int*, int*);
void display (int[], int*);

//void displayC (int[], int*);
void displayA (int[], int*);

//----------------------------------------------------
int pushA (int a[max], int *top, int *item)
{
    printf("\n\tEnter element: ");
    scanf("%d", &(*item));
    a[++(*top)] = (*item);
    printf("\n\tPush is successful...\n");
    return (*top);
}

//----------------------------------------------------
int cpyAC (int a[max], int c[max], int *top)
{

   int i= -1, j= -1, toc;
   while (j < *top)
      c[++i] = a[++j];
   toc= i;		
   return (toc);
}

//----------------------------------------------------
int palgen (int a[max], int b[maxim], int c[max], int *top, int *toc)
{
   int i, j, k, tob, x;
   j= -1;
   i= (*top);			
   k= (*toc);			

   while (k > -1)
   {

      b[++j] = c[k];

      (*toc)--;
      k= (*toc);

      while (b[j] != a[i])
      {
          i--;
      }

      while (i <= (*top))
      {

          c[++k] = a[++i];
         (*toc)++;
    
          i+= 0;
      }

      (*toc)--;
      k = (*toc);
      k+= 0;
   }
   tob= j;			
   return (tob);
}

//----------------------------------------------------
/*
void displayC (int c[max], int *toc)
{
   int i;
   printf("\n\tThe items of Stack are shown...{C}\n");
   i= (*toc);
   while (i >= 0)
   {
      printf("\n\t%d:\t%d", i, c[i--]);
   }
}
*/
//---------------------------------------------------
void displayA (int a[max], int *top)
{
   int i;
   printf("\n\tYou entered...\n");
   i= (*top);
   while (i >= 0)
   {
      printf("\n\t%d", a[i--]);
   }
}

//---------------------------------------------------
void display (int b[maxim], int *tob)
{
   int i;
   printf("\n\tThe items of Stack are shown...{B}\n");
   i= (*tob);
   printf("\n\n\t\t");
   while (i >= 0)
   {
      printf("%d", b[i--]);
   }
}

//----------------------------------------------------
void main (void)
{
   int a[max], b[maxim], c[max], item=0, top =(-1), toc, tob;
   char ch;
   system ("clear");
   printf ("\n\tThis is a Process of generating Palindrome number from some given numbers\n\n");

   if (top == -1)
      printf("\n\tInitially Stack is empty...\n");
  
   printf("\n\tYou have to give data, please start insertion.\n\tPress 'y' for insert, 'n' for exit.\n\n");
   ch= getchar();
   while (ch == 'y') 
   {
      top= pushA (a, &top, &item);
      printf("\n\t'y' :insert\n\t'n' :exit.\n");
      getchar();    
      ch= getchar();
      getchar();
      if( top == (max-1) )
      {
          printf("\n\tSorry...\n\tStack is full.\n\tPush operation is not possible...\n\n"); 
        break;
      } 
   }

   system("clear");
   displayA (a, &top);

   if(top == -1)
      printf("\n\tSorry...\n\tStack is empty...\n\tCopy is done...\n\n");
   else
     toc= cpyAC (a, c, &top);			

   //  displayC (c, &toc);

   tob= palgen (a, b, c, &top, &toc);		

   if(tob == -1)
      printf("\n\tSorry...\n\tStack is empty...\n\n");
   else
      display (b, &tob);

   printf ("\n\n\tT  H  A  N  K    Y  O  U\n\n");  
}
Arup Goswami
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date