ramytamer

palindrome

Apr 16th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define TYPE char
  7. #define STACKSIZE 100
  8.  
  9. typedef struct Stack {
  10.     int top;
  11.     TYPE items[STACKSIZE];
  12. }Stack;
  13.  
  14. void initialize(Stack *s){
  15.     s->top=0;
  16. }
  17.  
  18. void push(Stack *s, TYPE value){
  19.     s->items[s->top]=value;
  20.     s->top++;
  21. }
  22.  
  23. TYPE pop(Stack *s){
  24.     s->top--;
  25.     return s->items[s->top];
  26. }
  27.  
  28. int isFull(Stack *s){
  29.     return s->top==STACKSIZE;
  30. }
  31.  
  32.  
  33. int isEmpty(Stack *s){
  34.     return s->top == 0;
  35. }
  36.  
  37. Stack reverseStack(Stack s){ /* Function to reverse the stack to put it in another stack */
  38.     Stack n; initialize(&n);
  39.     while(!isEmpty(&s)) push(&n,pop(&s));
  40.     return n;
  41. }
  42.  
  43.  
  44. int main(){
  45.     system("clear");
  46.  
  47.     // Declaring Stack with name S & initialize it
  48.     Stack s; initialize(&s);
  49.     Stack n; initialize(&n);
  50.  
  51.     char x[STACKSIZE]; printf("Enter word to test if palindrome : "); gets(x);
  52.  
  53.     int i; for(i=0;x[i];i++) push(&s,x[i]);
  54.  
  55.     n = reverseStack(s);
  56.  
  57.     int flag=1;
  58.     while(!isEmpty(&s))
  59.         if(pop(&s) != pop(&n)) {
  60.             flag = 0; break;
  61.         }
  62.  
  63.     printf("%s is",x); flag ? printf(" ") : printf(" NOT "); printf("palindrome.\n");
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment