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 #define max 10#define maxim 30int 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("
	Enter element: ");    scanf("%d", &(*item));    a[++(*top)] = (*item);    printf("
	Push is successful...
");    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("
	The items of Stack are shown...{C}
");   i= (*toc);   while (i >= 0)   {      printf("
	%d:	%d", i, c[i--]);   }}*///---------------------------------------------------void displayA (int a[max], int *top){   int i;   printf("
	You entered...
");   i= (*top);   while (i >= 0)   {      printf("
	%d", a[i--]);   }}//---------------------------------------------------void display (int b[maxim], int *tob){   int i;   printf("
	The items of Stack are shown...{B}
");   i= (*tob);   printf("

		");   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 ("
	This is a Process of generating Palindrome number from some given numbers

");   if (top == -1)      printf("
	Initially Stack is empty...
");     printf("
	You have to give data, please start insertion.
	Press 'y' for insert, 'n' for exit.

");   ch= getchar();   while (ch == 'y')    {      top= pushA (a, &top, &item);      printf("
	'y' :insert
	'n' :exit.
");      getchar();          ch= getchar();      getchar();      if( top == (max-1) )      {          printf("
	Stack is full.
	Push operation is not possible...

");         break;      }    }   system("clear");   displayA (a, &top);   if(top == -1)      printf("
	Stack is empty...
	Copy is done...

");   else     toc= cpyAC (a, c, &top);			   //  displayC (c, &toc);   tob= palgen (a, b, c, &top, &toc);		   if(tob == -1)      printf("
	Stack is empty...

");   else      display (b, &tob);   printf ("

	T  H  A  N  K    Y  O  U

");  }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: