0% found this document useful (0 votes)
9 views4 pages

Applications of Stack

The document provides a C program that demonstrates the use of a stack data structure to reverse a string and check if it is a palindrome. It includes functions for creating a stack, pushing and popping elements, cleaning the string, and performing the palindrome check. The main function prompts the user for input and displays the original string, its reversed version, and whether it is a palindrome.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Applications of Stack

The document provides a C program that demonstrates the use of a stack data structure to reverse a string and check if it is a palindrome. It includes functions for creating a stack, pushing and popping elements, cleaning the string, and performing the palindrome check. The main function prompts the user for input and displays the original string, its reversed version, and whether it is a palindrome.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Applications of Stack

INPUT

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX_SIZE 1000

struct Stack {

int top;

unsigned capacity;

char* array;

};

struct Stack* createStack(unsigned capacity) {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (char*)malloc(stack->capacity * sizeof(char));

return stack;

int isFull(struct Stack* stack) {

return stack->top == stack->capacity - 1;

int isEmpty(struct Stack* stack) {

return stack->top == -1;

void push(struct Stack* stack, char item) {

if (isFull(stack))
return;

stack->array[++stack->top] = item;

char pop(struct Stack* stack) {

if (isEmpty(stack))

return '\0';

return stack->array[stack->top--];

void reverseString(char* str) {

int n = strlen(str);

struct Stack* stack = createStack(n);

for (int i = 0; i < n; i++)

push(stack, str[i]);

for (int i = 0; i < n; i++)

str[i] = pop(stack);

free(stack->array);

free(stack);

void cleanString(char* str) {

int i, j;

for (i = 0, j = 0; str[i]; i++) {

if (isalnum((unsigned char)str[i]))

str[j++] = tolower((unsigned char)str[i]);

str[j] = '\0';

int isPalindrome(char* str) {

char cleaned[MAX_SIZE];

strcpy(cleaned, str);

cleanString(cleaned);
int len = strlen(cleaned);

struct Stack* stack = createStack(len);

for (int i = 0; i < len / 2; i++)

push(stack, cleaned[i]);

if (len % 2 != 0)

len++;

for (int i = len / 2; i < len; i++) {

if (cleaned[i] != pop(stack)) {

free(stack->array);

return 0;

free(stack->array);

free(stack);

return 1;

int main() {

char str[MAX_SIZE];

printf("Enter a string: ");

fgets(str, MAX_SIZE, stdin);

str[strcspn(str, "\n")] = 0

printf("\nOriginal string: %s\n", str);

char reversed[MAX_SIZE];

strcpy(reversed, str);

reverseString(reversed);

printf("Reversed string: %s\n", reversed);

if (isPalindrome(str))

printf("\nThe string is a palindrome.\n");


else

printf("\nThe string is not a palindrome.\n");

return 0;

OUTPUT

Enter a string: Isha

Original string: Isha

Reversed string: ahsI

The string is not a palindrome.

You might also like