Question

In: Computer Science

Write a function that uses a local char queue and a local char stack to determine...

Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like:

#include <stack>

#include <queue>

...

bool isPalindrome(const string& candidate)

{

stack<char> s;

queue<char> q;

//add only upper case letters and digits to the stack and the queue

//if a letter is not upper case then convert it to upper case  

}

Note: I need help to write out this problem in C++ while using Visual Studio 2017

Solutions

Expert Solution

#include<iostream>
#include<queue>
#include<stack>
#include <cctype>

using namespace std;

bool isPalindrome(const string& candidate)
{
   stack<char> s;
   queue<char> q;
   
   //add only upper case letters and digits to the stack and the queue
   //if a letter is not upper case then convert it to upper case  
   for(int i = 0;i<candidate.length();i++){
      if(isupper(candidate[i]) || isdigit(candidate[i])){
         s.push(candidate[i]);
         q.push(candidate[i]);
      }
      else if(islower(candidate[i])){
         s.push(toupper(candidate[i]));
         q.push(toupper(candidate[i]));
      }
   }

   while(!s.empty()){
      if(s.top() != q.front()){
         return false;
      }
      s.pop();
      q.pop();
   }
   return true;
}

int main() {
   //Testing isPalindrome function 
   cout<<isPalindrome("abcddcba")<<endl;
   cout<<isPalindrome("abcdcba")<<endl;
   cout<<isPalindrome("abdcba")<<endl;
   return 0;
}

1
1
0


Related Solutions

Q1 A- What are stack and queue? What are the differences between stack and queue? B-...
Q1 A- What are stack and queue? What are the differences between stack and queue? B- What is the priority queue? What are the differences between queue and priority queue
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
Compare the list, stack and queue
Compare the list, stack and queue
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
For this exercise, you will be defining a function which USES both the Stack and the...
For this exercise, you will be defining a function which USES both the Stack and the Queue ADTs. Your code can make use of any of the Queue ADT methods: Queue(), enqueue(), dequeue(), peek(), size() and is_empty() and any of the Stack ADT methods: Stack(), push(), pop(), peek(), size() and is_empty(). Write a function called mirror_queue(a_queue) which takes a Queue as a parameter. The function must modify the parameter Queue object so that the original queue items appear in their...
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT