In: Computer Science
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as a palindrome, since it can be reduced as “madamimadam”. [Hint: store characters both in a queue and onto a stack]
ANS:
#include<string>
#include<stack>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
stack<char> obj;
queue<char> obj2;
void push(char* str, int cSize)
{
for (int i = 0; i < cSize;i++)
{
obj.push(str[i]);
}
}
void pushQueue(char* str, int cSize)
{
for (int i = 0; i < cSize; i++)
{
obj2.push(str[i]);
}
}
bool checkPlaindrome()
{
while (!obj.empty() || !obj2.empty())
{
if (obj.top() !=
obj2.front())
{
return
false;
}
obj.pop();
obj2.pop();
}
return true;
}
int main()
{
char* charS;
string str;
cout << "Enter the string you want to test"
<< endl;
cin >> str;
charS = new char[str.size()+1];
for (int i = 0; i < str.size() + 1; i++)
{
charS[i] = str[i];
}
cout << charS << endl;
cout << str.size() << endl;
push(charS, str.size());
pushQueue(charS, str.size());
if (checkPlaindrome())
{
cout << "It is plaindrome"
<< endl;
}
else
{
cout << "Not Plaindrome"
<< endl;
}
}
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!