You are on page 1of 4

/*This program will recognise a string input by user is in the language L, where L = {w$w': w is a possibly empty string of characters

other than $, w' = reverse(w)}*/ #include <stdio.h> /* Define standard input output routine */ #include <stdlib.h> #include <conio.h> /* Declaration of Stack data structure */ #define MAXSTACK 5 // Size of Stack typedef enum bool { FALSE, TRUE } BOOL; typedef struct stack{ int items[MAXSTACK]; int top; } STACK; /* Declaration of function prototypes */ void createStack(STACK *p); BOOL EmptyStack (STACK *p); BOOL FullStack (STACK *p); void InsertStack(STACK *p, int element); int RemoveStack(STACK *p);

int main(){ STACK stck; createStack(&stck); char str[0];

int i, flag; stck.top = -1;

printf("Enter a string: "); //Prompt user to input a string gets(str); //read the user's input

for(i = 0; str[i] != '\0'; i++){ InsertStack(&stck, str[i]); flag = 1; }//end of for

for(i = 0; str[i] != '$'; i++){ $ will act as mirror between if(str[i] != RemoveStack(&stck)){ //left and right char of the string flag = 0; break; } }//end of for

//Character

if(flag == 1) printf("\nResult: The above string is in the language\n\n");

else

printf("\nResult: The above string is not in the language\n\n");

getchar(); return 0; }//end of main

void createStack(STACK *p){ //Function to create empty stack p->top = 0; }

BOOL EmptyStack (STACK *p){

//Check if the stack is empty

return ((BOOL) (p->top<=0) ); }//end of BOOL EmptyStack

BOOL FullStack (STACK *p){ // Check if the stack is full return ((BOOL) (p->top >= MAXSTACK - 1)); }//end of BOOL FullStack

/* Insert an item into a stack t or display an error message if t is full */ void InsertStack(STACK *p, int element){ if(FullStack(p)){ printf("stack is full !\n"); } else{

(p->top)++; p->items[p->top] = element; } }//end of void push

/* Remove an item from a stack t or display an error message if t is empty */ int RemoveStack(STACK *p){ if(EmptyStack(p)){ printf("stack empty !\n"); } else if (p->top > 0){ return (p->items[(p->top)--]); } }//end of void pop

You might also like