Fds Pract No 11 (Group'D')

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Name:- Jayraj Yuvraj Khamkar

Roll no.:-15 Div:-'B'

Sub:-FDS

Practical no. 11(group'B')


Aim:
Write a C++ program to check whether the given string
is palindrome or not using stack using array.
Objectives:
1. To study stack using array
2. To check whether the given string is palindrome or not
Problem Statement:
A palindrome is a string of character that‘s the same forward
and backward. Typically, punctuation, capitalization, and
spaces are ignored. For example, ‖Poor Dan is in a droop‖ is a
palindrome, as can be seen by examining the characters
―poor danisina droop‖ and observing that they are the same
forward and backward. One way to check for a palindrome is
to reverse the characters in the string and then compare with
them the original-in a palindrome, the sequence will be
identical. Write C++ program with functions-
1. To check whether given string is palindrome or not that
uses a stack to determine whether a string is a palindrome.
2. To remove spaces and punctuation in string, convert all the
Characters to lowercase, and then call above Palindrome
checking function to check for a palindrome
3. To print string in reverse order using stack
Software Requirements:
Ubuntu Linux 14.04, GCC / G++ (editor)
Hardware Requirements:
Any processor above Pentium 4
Theory and Concept:
1. Stack:
1. Stack is a LIFO(last in first out) structure. It is an
ordered list of the same type of elements.
2. A stack is a linear list where all insertions and
deletions are permitted at only one end of list.
3. When elements are added to stack it grows at one
end. Similarly, when elements are deleted from
stack, it shrinks at the same end.
2. Stack Using Array:
1. Stack is a LIFO structure. Stack can be
represented using array.
2. A one dimensional array can be used to hold
elements of stack.
3. Another variable “top” is used to keep track of the
index of the top most element.
4. The following operations can be done on the stack
by using array:
a) Initialization Of Stack:
#include<iostrem>
#include<string.h>
#define MAX 100
using namespace std;
typedefstruct stack
{
Char data[MAX];
int top;
}stack;
voidinit(stack *stck)
{
Char data[MAX];
int top;
}stack;
voidinit(stack *stck)
{
int I;
for(i=0;i<MAX;i++)
stck->data[i]=’\0’;
stck->top=-1;
}
b) Print Function:
void print(stack stck)
{
int i;
cout<<”\nStack elements are::”;
for(i=0;i<MAX;i++)
cout<<stck.data[i];
cout<<”\ttop=”<<stck.top;
}

c) Isempty Condition:
intisempty(stack stck)
{
returnstck.top==-1?1:0;
}
d) Isfull Condition:
intisfull(stack stck)
{
returnstck.top==MAX-1?1:0;
}
e) Push Function:
for(i=0;data[i]!=’\0’;i++)
{
stck->top+=1;
stck->data[stck->top]=data[i];
}
f) Pop Function:

3. Palindrome:
A palindrome is a word, sentence, or number that reads
the same from left to right as from right to left. In this
program, we take two cases to check whether the string is
palindrome or not:
a) Keep the string as it is:
As given string “Poor Dan is in a droop” is reversed as it
is, it becomes “poord a nisinaDrooP” which is not
similar to given string. Therefore, given string is not
palindrome.
b) Remove all spaces, punctuations and convert uppercases
into lowercases:
Given string is “Poor Dan is in a droop”. If we
convert all uppercases into lower cases it becomes
“poor dan is in a droop”. Now remove all punctuations
and spaces it becomes “poordanisinadroop”.
If we reversed this string it becomes
“poordanisinadreoop” which is similar to that.
Therefore, given string is palindrome.

init(&stck);
for(i=0;data[i]!=’\0’;i++)
{
if(data[i]!=’ ‘)
{
if(data[i]>=65 && data[i]<=90)
data1[j]=data[i]+32;
else
data1[j]=data[i];
j++;
}
}

Algorithm:
Step 1: Start
Step 2: Declare characters data[MAX] and data1[MAX].
Step 3: Declare integers top, i, j, ch
Step 4: Accept the string from user
Step 5: Check given string is palindrome or not and display it.
If it is palindrome then gotostep 8 elsegoto step 6.
Step 6: Change upper cases to lower cases and remove all
spaces
Step 7: Again check the string is palindrome or not and
display it
Step 8: Stop
Source code
/reverse of a string by using stack implementation and check
wheather string is palindrome or not/
#include<iostream>
using namespace std;
class stack
{
char str[40]; //declaration of stack
int top,n;
public:
stack()
{
top=-1;
n=0;
}
void push();
char pop();
void display();
void checkpalindrome();
};
void stack::push()
{
char p;
cout<<"how many character does the string contains:";
cin>>n;
if(top==n-1)
{
cout<<"\nstack overflow "<<endl;
}
else
{
for(int i=0;i<n;i++)
{
cout<<"enter character "<<i+1<<":";
cin>>p;
top++;
str[top]=p;
}
}
}
char stack::pop()
{ char op[40];
char r=' ';
for(int i=0;i<n;i++)
{
if(top==-1)
{
cout<<"stack underflow"<<endl;
}
else
{
r=str[top];
top=top-1;
cout<<"element deleted is:"<<r<<endl;
op[i]=r;
}
}
cout<<"reverse of the string is:"<<op<<endl;
}
void stack::display()
{
for(int i=0;i<=top;i++)
cout<<str[i];
cout<<endl;
}
void stack::checkpalindrome()
{
int j=n-1;
bool t=true;
for(int i=0;i<n/2;i++)
{
if(str[i]!=str[j])
t=false;
else
{ i++;
j--;
}
}
if(t)
cout<<"string is palindrome..."<<endl;
else
cout<<"string is not palindrome..."<<endl;
}

int main()
{
int ch;
char a[40];
char k;
stack s;
do
{
cout<<"1.enter string\n";
cout<<"2.display string\n";
cout<<"3.reverse string\n";
cout<<"4.check for palindrome\n";
cout<<"5.exit\n";
cout<<"enter your choice:";
cin>>ch;
switch(ch)
{
case 1:s.push();
break;
case 2: cout<<"characters u have entered are:";
s.display();
break;
case 3: s.pop();
break;
case 4:s.checkpalindrome();
break;
case 5:exit(0);
default: cout<<"wrong input\n";
}
} while(ch!=5);

return 0;
}
Output
Thank You!!!

You might also like