Вы находитесь на странице: 1из 9

/*

* C Program to Identify whether the String is Palindrome or not using Stack

*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 50

int top = -1, front = 0;

int stack[MAX];

void push(char);

void pop();

void main()

int i, choice;

char s[MAX], b;

while (1)

printf("1-enter string\n2-exit\n");

printf("enter your choice\n");

scanf("%d", &choice);

switch (choice)

case 1:
printf("Enter the String\n");

scanf("%s", s);

for (i = 0;s[i] != '\0';i++)

b = s[i];

push(b);

for (i = 0;i < (strlen(s) / 2);i++)

if (stack[top] == stack[front])

pop();

front++;

else

printf("%s is not a palindrome\n", s);

break;

if ((strlen(s) / 2) = = front)

printf("%s is palindrome\n", s);

front = 0;

top = -1;

break;
case 2:

exit(0);

default:

printf("enter correct choice\n");

/* to push a character into stack */

void push(char a)

top++;

stack[top] = a;

/* to delete an element in stack */

void pop()

top--;

Program Explanation

1. Take a string as input and store it in the array s[].

2. Load each character of the array s[] into the array stack[].

3. Use variables front and top to represent the last and top element of the array stack[].

4. Using for loop compare the top and last element of the array stack[]. If they are equal, then delete the
top element, increment the variable front by 1 and compare again.
5. If they are not equal, then print the output as “It is not a palindrome”.

6. Compare the elements in the steps 4-5 upto the middle element of the array stack[].

7. If every characters of the array is equal, then it is a paliandrome.

advertisement

Runtime Test Cases

1-enter string

2-exit

enter your choice

Enter the String

madam

madam is palindrome

1-enter string

2-exit

enter your choice

Enter the String

ugesh

ugesh is not a palindrome

1-enter string

2-exit

enter your choice

1
Enter the String

abccba

abccba is palindrome

1-enter string

2-exit

enter your choice

Enter the String

abdbca

abdbca is not a palindrome

1-enter string

2-exit

enter your choice

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Stacks & Queues, go to C Programming Examples on
Stacks & Queues. If you wish to look at programming examples on all topics of C, go to C Programming
Examples.

« Prev Page - C Program to Print the Range of Fundamental Data Types

» Next Page - C Program to Print Border of given Tree in Anticlockwise Direction

Deep Dive @ Sanfoundry:

C Programming Examples on Bitwise Operations


C Programming Examples

C# Programming Examples on Exceptions

C++ Programming Examples on Set & String Problems & Algorithms

C Programming Examples on Data-Structures

C++ Programming Examples on STL

C Programming Examples on Set & String Problems & Algorithms

C Programming Examples using Recursion

C# Programming Examples on Data Structures

C Programming Examples on Stacks & Queues

Manish Bhojasia

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at
Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency
developments in these areas. He lives in Bangalore and delivers focused training sessions to IT
professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage,
Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI &
Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks

SAN Developer

Linux Kernel Developer

Linux Driver Developer

Linux Network Developer

Live Training Photos

Mentoring

Software Productivity
GDB Assignment

Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our
Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle,
Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio,
Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata
VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology

SAN II - Admin

Linux Fundamentals

Advanced C Training

Linux-C Debugging

System Programming

Network Programming

Linux Threads

Kernel Programming

Kernel Debugging

Linux Device Drivers

Best Reference Books

Computer Science Books

Algorithm & Programming Books

Electronics Engineering Books

Electrical Engineering Books

Chemical Engineering Books

Civil Engineering Books

Mechanical Engineering Books

Industrial Engineering Books


Instrumentation Engg Books

Metallurgical Engineering Books

All Stream Best Books

Questions and Answers

1000 C Questions & Answers

1000 C++ Questions & Answers

1000 C# Questions & Answers

1000 Java Questions & Answers

1000 Linux Questions & Answers

1000 Python Questions

1000 PHP Questions & Answers

1000 Hadoop Questions

Cloud Computing Questions

Computer Science Questions

All Stream Questions & Answers

India Internships

Computer Science Internships

Instrumentation Internships

Electronics Internships

Electrical Internships

Mechanical Internships

Industrial Internships

Systems Internships

Chemical Internships

Civil Internships
IT Internships

All Stream Internships

About Sanfoundry

About Us

Copyright

Terms

Privacy Policy

Jobs

Bangalore Training

Online Training

Developers Track

Mentoring Sessions

Contact Us

Sitemap

© 2011-2019 Sanfoundry. All Rights Reserved.

Sponsored

Advertisement

Sponsored

Advertisement

Вам также может понравиться