CHECK WHETHER STRING IS PALINDROME USING STACK.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
using namespace std;
template <class Type>
class Palindrom
{
private:
int Top, Size;
Type *Arr;
public:
Palindrom(int Siz)
{
Top = -1;
Size = Siz;
Arr = new Type[Size];
strcpy(Arr, "\0");
}
void Push(Type Ch)
{
if(Top == Size-1)
{
cout << "\nStack Overflowed..........." << endl;
exit(1);
}
Arr[++Top] = Ch;
}
Type Pop()
{
if(isEmpty())
{
cout << "\nStack Underflowed............" << endl;
exit(1);
}
return Arr[Top--];
}
inline bool isEmpty()
{
return (Top == -1);
}
void Read_Check(char *Arrays)
{
for(int I =0; I <strlen(Arrays); I++)
{
char Ch = Arrays[I];
Push(Ch);
}
for(int I =0; I <strlen(Arrays); I++)
Arrays[I] = Pop();
if(strcmp(Arr, Arrays) == 0)
cout << "\nString is Palindrome.\n" << endl;
else
cout << "\nIt is Not Palindrome.\n" << endl;
}
~Palindrom()
{
cout << "\n\n\n----------PROGRAM ENDED---------------\n\n\n" << endl;
}
};
int main()
{
char Array[100];
Palindrom<char> Obj1(5);
cout << "Enter Your String: ";
cin >> Array;
Obj1.Read_Check(Array);
getch();
return 0;
}
0 comments:
Post a Comment