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

C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
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[] =...
Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms...
Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms a triangle. The return string should contain: one instance of char on the first line, two instances of char on the second line, and so on up to n instances of char on line n. Then n-1 instances of char on line n+1, and so on until the triangle is complete. Sample runs of the function looks as follows (this function called from IDLE,...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT