3 Replies - 285 Views - Last Post: 24 November 2013 - 11:13 AM Rate Topic: -----

#1 Rbroke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 13

palindrome program

Posted 24 November 2013 - 10:13 AM

The instructions for the program is

bool isPalindrome = true;

queue<char> Q;
stack<char> S;
string str;
cout<< "Enter a word : ";
getline(cin, str, '\n');

2. enqueue the string into the queue

3. push the string into the stack

4. while the stack is not empty and the queue is not empty do the followings:

if the front of the queue is not equal to the top of the stack:
dequeue the queue;
pop the stack;
isPalindrome=false;
else
dequeue the queue;
pop the stack;
5. if (isPalindrome)
cout << str << " is a palindrome" << endl;
else
cout << str << " is not a palindrome " << endl;

I believe the program does that but only checks the first and last letters, how do I get it to check the rest and accurately say it is or isn't a Palindrome.

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


using namespace std;

int main()
{
	string str;
	int i;
	queue<char> Q;
	stack<char> S;
	bool isPalindrome = true;

	cout <<"Enter a text in lowercase: "<< endl;
	getline(cin, str);
	
	for (i = 0; i < str.length(); i++) {
			str[i]= str.at(i);
	}
	
	for (i = 0; i < str.length(); i++) {
		Q.push(str[i]);
		S.push(str[i]);
	}

	for (i = 0; i < str.length(); i++) {
	
	if (Q.front() != S.top())
	{
		Q.pop();
		S.pop();
		isPalindrome=false;
	}
	else{
		Q.pop();
		S.pop();
		
	}


	if(!isPalindrome) {
			cout << str <<"  is not a palindrome."<<endl;
			break;
	}
	else{
		cout << str <<"  is a palindrome. " << endl;
			break;
		}
	}
	
	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: palindrome program

#2 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 229
  • Joined: 07-July 13

Re: palindrome program

Posted 24 November 2013 - 10:48 AM

eleminate the lines between no.20,21,22 as they are useless
there is a misplaced close bracket for the for loop at line no. 52.remove and place it at line no. 42..
and insert the break at line no.35 next to ispalindrone=false;
and remove all the breaks in the if else statement

This post has been edited by yashwanth.c.b: 24 November 2013 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 Rbroke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 13

Re: palindrome program

Posted 24 November 2013 - 11:01 AM

Figured it out, Thanks anyways
Was This Post Helpful? 0
  • +
  • -

#4 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 229
  • Joined: 07-July 13

Re: palindrome program

Posted 24 November 2013 - 11:13 AM

your welcome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1