Answer to Question #322365 in C++ for Adnan

Question #322365

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-07-01T07:46:35-0400
#include <iostream>
#include <queue>
#include <stack>

int main()
{
  std::queue<char> myQueue;
  std::stack<char> myStack;

  while (true)
  {
    char c;
    std::cin.get(c);
    if (std::cin.eof() || c == '\r' || c == '\n')
      break;
    myStack.push(c);
    myQueue.push(c);
  }
  int size = myStack.size();
  bool result = true;
  for (int i = 0; i < size; i++)
  {
    char queue_char = myQueue.front();
    myQueue.pop();
    char stack_char = myStack.top();
    myStack.pop();
    if (stack_char != queue_char)
    {
      result = false;
      break;
    }
  }
  std::cout << (result ? "true" : "false");
	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
APPROVED BY CLIENTS