Fds Pract No 11 (Group'D')
Fds Pract No 11 (Group'D')
Fds Pract No 11 (Group'D')
Sub:-FDS
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!!!