Palindrome Using C Programming
Palindrome Using C Programming
Palindrome Using C Programming
other than $, w' = reverse(w)}*/ #include <stdio.h> /* Define standard input output routine */
/* Declaration of Stack data structure */ #define SIZE 10 /* Size of Stack */ typedef struct{ int items[SIZE]; int top; } STACK; /* Declaration of function prototypes */ void push(); int pop(); void display(); int is_overflow(); int is_empty();
int main(){ STACK stck; char str[100]; int a, flag; stck.top = -1;
printf("This program will check whether the string is in language\n"); printf("L = {w$w': w is a possibly empty string of characters \n"); printf("other than $, w' = reverse(w)}\n"); printf("======================================================== =\n"); printf("Enter a string: "); /* Prompt user to input */ gets(str);
flag = 1; for(a=0;str[a]!='$';a++){ will act as mirror between */ if(str[a] != pop(&stck)){ char of the string */ flag = 0; break; } } if(flag == 1){ printf("\nThe above string is in the language\n"); } else{ /* Character $ /* left and right
//FUNCTIONS void push(STACK *p, int element){ operation */ (p->top)++; p->items[p->top] = element; } /* Function for PUSH
/*Function to check stack overflow condition */ int is_overflow(int top){ if(top == SIZE - 1) return(1); else return(0); }
/* Function to check stack empty condition */ int is_empty(int top){ if(top == -1) return(1); else return(0); }
/* Write a C program to implement stack. Stack is a LIFO data strcuture * * LIFO - Last in First Out * * Perform PUSH(insert operation), POP(Delete operation) and Display stack */ #include <stdio.h> #include <conio.h> #define MAXSIZE 5
struct stack {
/* Function declaration/Prototype*/
int main ()
s.top = -1;
printf ("STACK OPERATION\n"); while (option) { printf ("-----------------------------------------\n"); printf (" \n"); printf (" \n"); printf (" \n"); printf (" -------\n"); 4 --> EXIT \n"); 3 --> DISPLAY 2 --> POP 1 --> PUSH
printf ("-----------------------------------
fflush (stdin); printf ("Do you want to continue(Type 0 or 1)?\n"); scanf } getch(); return 0; } ("%d", &option);
/*Function to add an element to the stack*/ void push () { int num; if (s.top == (MAXSIZE - 1)) { printf ("Stack is Full\n"); return; }
else { printf ("Enter the element to be pushed\n"); scanf ("%d", &num); s.top = s.top + 1; s.stk[s.top] = num; } return; }
/*Function to delete an element from the stack*/ int pop () { int num; if (s.top == - 1) { printf ("Stack is Empty\n"); return (s.top); } else { num = s.stk[s.top]; printf ("poped element is = %d\n", s.stk[s.top]); s.top = s.top - 1;
} return(num); }
/*Function to display the status of the stack*/ void display () { int i; if (s.top == -1) { printf ("Stack is empty\n"); return; } else { printf ("\nThe status of the stack is\n"); for (i = s.top; i >= 0; i--) { printf ("%d\n", s.stk[i]); } } printf ("\n"); }
Output STACK OPERATION -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY 4 --> EXIT -----------------------------------------Enter your choice 1 Enter the element to be pushed 23 Do you want to continue(Type 0 or 1)? 1 -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY 4 --> EXIT -----------------------------------------Enter your choice 1 Enter the element to be pushed 45 Do you want to continue(Type 0 or 1)? 1 -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY 4 --> EXIT -----------------------------------------Enter your choice 1 Enter the element to be pushed 78 Do you want to continue(Type 0 or 1)? 1 -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY
4 --> EXIT -----------------------------------------Enter your choice 3 The status of the stack is 78 45 23 Do you want to continue(Type 0 or 1)? 1 -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY 4 --> EXIT -----------------------------------------Enter your choice 2 poped element is = 78 Do you want to continue(Type 0 or 1)? 1 -----------------------------------------1 --> PUSH 2 --> POP 3 --> DISPLAY 4 --> EXIT -----------------------------------------Enter your choice 3 The status of the stack is 45 23 Do you want to continue(Type 0 or 1)? 0
}STACK;
void push(); int pop(); void display(); int isoverflow(); int isempty();
s.top = -1;
getch(); return 0;
void push(STACK *p, int element) { if(isoverflow(p->top)) { printf("\nStack is overflow"); } else { (p->top)++; p->items[p->top] = element; } }
void display(STACK s) { int i; if(isempty(s.top)) { printf("\nStack is empty"); } else { for(i=s.top; i>=0; i--) { printf("\n%d", s.items[i]); } } }
//Function to check stack overflow condition int isoverflow(int top) { if(top == SIZE - 1) return (1); else return (0);
//Function to check stack empty condition int isempty(int top) { if(top == -1) return (1); else return (0); }
// This program determine if a word is a palindrome or not using stack and pointer.
#define SIZE 50
//pointers declarations and stack declaration char stack[SIZE]; char *top = stack; char *bottom = stack;
int i; //push characters onto stack for (i = 0; i < strlen(string); i++) { if(isalnum(string[i]) && !isspace(string[i])) //if a character in the string s is a digit or alphabet character and not a white space { char c = tolower(string[i]); push(c);//push onto stack } }
/* visualize the stack printf("Your stack currently has: "); for(i=0; i < strlen(string); i++){ char temp=stack[i]; printf("%c", temp); } printf("\n");*/
// pop the stack for(i=0; i < strlen(string); i++){ char currItem = pop();
if(currItem != *bottom){ printf("NOT a palindrome.\n"); return 0; } else{ bottom++; } } printf("This is a palindrome.\n"); getch(); return 0; }