Answer to Question #322364 in C++ for Adnan

Question #322364

A palindrome is a string that reads the same forwards as backwards. Using



only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed



number of int and char variables, write a code to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The code should output true or false as appropriate.



1
Expert's answer
2022-06-30T15:19:37-0400
#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main()
{
  stack<char> stack_;
  queue<char> queue_;

  while (true)
  {
    char c;
    cin.get(c);
    if (cin.eof() || c == '\r' || c == '\n')
      break;
    stack_.push(c);
    queue_.push(c);
  }
  int size = stack_.size();
  for (int i = 0; i < size; i++)
  {
    char stack_char = stack_.top(); stack_.pop();
    char queue_char = queue_.front(); queue_.pop();
    if (stack_char != queue_char)
    {
      cout << "false";
      return 0;
    }
  }
  cout << "true";
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog