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

C++ Stack Program for Palindrome Check

The document contains a C++ program that implements a stack class to check for palindromes and reverse strings. It includes methods for pushing and popping characters from the stack, as well as functions to convert strings to lowercase. The main function provides a menu for the user to choose between checking for a palindrome, reversing a string, or exiting the program.

Uploaded by

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

C++ Stack Program for Palindrome Check

The document contains a C++ program that implements a stack class to check for palindromes and reverse strings. It includes methods for pushing and popping characters from the stack, as well as functions to convert strings to lowercase. The main function provides a menu for the user to choose between checking for a palindrome, reversing a string, or exiting the program.

Uploaded by

tm54hrfccn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 10

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#define SIZE 30

using namespace std;

class mystack
{
private :
char ST[SIZE];
int top;
public :
mystack();
void push(char X);
char pop();
int isEmpty();
int isFull();
};

mystack :: mystack()
{
top = -1;
}

void mystack :: push(char X)


{
if(!isFull())
{
top++;
ST[top] = X;
}
else
cout<<"\nStack Overflow !! Error!!";
}

char mystack :: pop()


{
char X = '\0';
if(!isEmpty())
{
X = ST[top];
top--;
}
return X;
}

int mystack :: isEmpty()


{
if(top == -1)
return 1;
else
return 0;
}
int mystack :: isFull()
{
if(top == SIZE-1)
return 1;
else
return 0;
}

void convert_string(char Str[],char Str1[])


{
int i,j = 0;
for(i=0;Str[i] != '\0';i++)
{
if(Str[i] >= 'a' && Str[i] <= 'z')
Str1[j++] = Str[i];
if(Str[i] >= 'A' && Str[i] <= 'Z')
Str1[j++] = Str[i] + 32;
}
Str1[j] = '\0';
}
int main()
{
int ch,flag,i;
char Str[80],Str1[80];
mystack S;
system("clear");
do
{
cout<<"\n\t\t\t1 : Check for Palindrome";
cout<<"\n\t\t\t2 : Find Reverse";
cout<<"\n\t\t\t3 : Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"\nEnter the string to be checked for
palindrome : ";
[Link]();
[Link](Str,79);
cout<<"\nEntered String is "<<Str;
convert_string(Str,Str1);
cout<<"\nconverted String is : "<<Str1;
for(i = 0; Str1[i] != '\0';i++)
[Link](Str1[i]);
i = 0; flag = 1;
while(![Link]())
{
if(Str1[i++] != [Link]())
flag = 0;
}
if(flag == 1)
cout<<"\nGiven string is a palindrome\n";
else
cout<<"\nGiven String is not a
palindrome\n";
break;
case 2 : cout<<"\nEnter the string to be reversed : ";
[Link]();
[Link](Str,79);
cout<<"\nString entered is "<<Str;

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


[Link](Str[i]);
cout<<"\nReverse String = ";
while(![Link]())
{
cout<<[Link]();
}
break;
case 3 : cout<<"\nEnd of Program\n";
break;
default: cout<<"\nInvalid choice !! Try again\n\n";
}
}while(ch != 3);
return 0;
}
Output
1 : Check for Palindrome
2 : Find Reverse
3 : Exit

Enter your choice : 1

Enter the string to be checked for palindrome : Poor Dan is in a droop

Entered String is Poor Dan is in a droop


converted String is : poordanisinadroop
Given string is a palindrome

1 : Check for Palindrome


2 : Find Reverse
3 : Exit

Enter your choice : 2

Enter the string to be reversed : Poor Dan is in a droop

String entered is Poor Dan is in a droop


Reverse String = poord a ni si naD rooP
1 : Check for Palindrome
2 : Find Reverse
3 : Exit

Enter your choice :3


End of Program

You might also like