Hello having some issues with my logic. I have to write a function that determines whether a given string is palindrome. I have to use multiple stacks for implementation. I have began coding and it compiles however it is not working correctly. I think the problem is within my while loop here is the code

#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string str;
	stack<int> s1;
	stack<int> s2;
	

	cout << "This program will determine if a given string is a palindrome." <<endl;
	cout << "Enter a string of characters from 1-100." << endl;
	getline(cin, str);
	for(int i = 0;i<(int)s1.size();i++)s1.push(i);


	while(!s1.empty()) // this part reads stackOne into stackTwo.
	{                         
		s1.pop();//Removes the first character.              
		//s2.push();		
		}

	if(s1 == s2)
	{
		cout << "It is a palindrome." << endl;
	}
	else
		cout << "It is not a palindrome." << endl;

	system("PAUSE");
	return 0;
}

Recommended Answers

All 5 Replies

> // this part reads stackOne into stackTwo.
But all you end up with is two empty stacks.

Try with both starting off in the same state. Then reverse one of them.

The pop() command does not return the value popped, so first you need to put the top element of stack1 onto stack2, then pop it from stack 1

Still, your logic won't work at all, because you are simply moving everything from one stack to the other. You need to think about how you can determine if the two halves of the word are the same but reversed and how you can use stacks to solve the problem.

Even if you get the code to work... The first stack will be empty while the second stack will have the letters in reversed order.

So this code would always return False!!

OK I've tried to change my logic. Is this closer to what I'm trying to achieve.

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
	string str1;
	string::reverse_iterator str2;
	stack<int> s1;
	stack<int> s2;

	cout << "This program will determine if a given string is a palindrome." <<endl;
	cout << "Enter a string of characters here:." << endl;
	cin >> str1;
	for( size_t i = 0; i < str1.length(); i++)
		s1.push(str1[i]);

	/*bool is_palindrome(const s1&, const s2&)
	{
	for ( str2=str1.rbegin() ; str2 < str1.rend(); str2++ )
	s2.push(str2);
	return s1 == s2
	}
*/
	/*

	if (is_palindrome = 0)
	cout << "It is a palindrome." << endl;
	else
	cout << "It is NOT a palindrome." << endl;*/

	system("PAUSE");
	return 0;
	}

OK I've tried to change my logic. Is this closer to what I'm trying to achieve.

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
	string str1;
	string::reverse_iterator str2;
	stack<int> s1;
	stack<int> s2;

	cout << "This program will determine if a given string is a palindrome." <<endl;
	cout << "Enter a string of characters here:." << endl;
	cin >> str1;
	for( size_t i = 0; i < str1.length(); i++)
		s1.push(str1[i]);

	/*bool is_palindrome(const s1&, const s2&)
	{
	for ( str2=str1.rbegin() ; str2 < str1.rend(); str2++ )
	s2.push(str2);
	return s1 == s2
	}
*/
	/*

	if (is_palindrome = 0)
	cout << "It is a palindrome." << endl;
	else
	cout << "It is NOT a palindrome." << endl;*/

	system("PAUSE");
	return 0;
	}

The logic here will work, but it's hardly efficient. You're making an entire copy of the input string in reverse and then comparing the elements of each. For the copy and compare logic, you should only copy half of the input and check. This is still not best. There really is no reason to copy the data. You can simply with an index to the front and back, stepping each toward the middel, and if you find a single missmatch, you are done.

Of course this breaks the requirement for using stacks to solve the problem. Because you have to use stacks, I would concentrate on figuring out how you can use two stacks to compare the two halves of the input string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.