0% found this document useful (0 votes)
193 views

Palindrome Using C Programming

The document describes a C program that uses a stack to check if a string is a palindrome by pushing the characters of the string to the stack and then popping them off and comparing to the original string. It defines stack functions like push(), pop() and displays the stack. It prompts the user to enter a string, pushes it to the stack, then pops and checks if each character matches the original to determine if it is a palindrome.

Uploaded by

Marilyn Low
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Palindrome Using C Programming

The document describes a C program that uses a stack to check if a string is a palindrome by pushing the characters of the string to the stack and then popping them off and comparing to the original string. It defines stack functions like push(), pop() and displays the stack. It prompts the user to enter a string, pushes it to the stack, then pops and checks if each character matches the original to determine if it is a palindrome.

Uploaded by

Marilyn Low
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

/*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 */

/* 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);

for(a=0;str[a]!='\0'; a++) push(&stck, str[a]);

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

printf("\nThe above string is not in the language\n"); } getchar(); return 0; }//main

//FUNCTIONS void push(STACK *p, int element){ operation */ (p->top)++; p->items[p->top] = element; } /* Function for PUSH

int pop(STACK *p){ return (p->items[(p->top)--]); operation */ } /* Function for POP

/*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 {

/* Structure definition for stack */

int stk[MAXSIZE]; int top; };

typedef struct stack STACK; STACK s;

/* Function declaration/Prototype*/

void push (void); int pop(void);

void display (void);

int main ()

{ int choice; int option = 1;

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 ("-----------------------------------

printf ("Enter your choice\n"); scanf ("%d", &choice);

switch (choice) { case 1: push();

break; case 2: pop();

break; case 3: display();

break; case 4: } return;

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

#include <stdio.h> #include <stdlib.h> #include <conio.h> #define SIZE 10

typedef struct { int items[SIZE]; int top;

}STACK;

void push(); int pop(); void display(); int isoverflow(); int isempty();

int main() { STACK s; char str[100]; int i, flag;

s.top = -1;

printf("\nEnter a string: "); gets(str);

for(i=0;str[i]!='\0'; i++) push(&s, str[i]);

flag = 1; for(i=0;str[i]!='\0';i++) { if(str[i] != pop(&s)) { flag = 0; break; } }

if(flag == 1) printf("\nEntered string is palindrome\n"); else printf("\nEntered string is not a palindrome\n");

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; } }

int pop(STACK *p) { if(isempty(p->top)) { printf("\nStack is underflow"); } else { return (p->items[(p->top)--]); }

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.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>

#define SIZE 50

//function declarations void push(char i); char pop(void);

//pointers declarations and stack declaration char stack[SIZE]; char *top = stack; char *bottom = stack;

int main(){ //declare a char array of size char string[SIZE];

printf("Enter string: "); scanf("%s", &string);

//printf("Your string is: %s \n", &string );

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; }

// push function void push(char i){ *top = i; top++; }

// pop function char pop(void){ top--; return *top; }

You might also like