Question

In: Computer Science

****C++, put both of them in to one main method, make sure to start with main...

****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well.

thank you

(1) 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 an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate

(2) Let Q be a non-empty queue, and let S be an empty stack. Using only the stack and queue ADT functions and a single element variable X, write an algorithm to reverse the order of the elements in Q.

Solutions

Expert Solution

Algorithm:

  1. First of all, we take the input.
  2. After that, we push that given string to both stack and a queue.
  3. For checking if the given string is the palindrome or not, we do pop operation from both stack and the queue and match the characters.
  4. If the characters are the same, we increase the counter.
  5. When the counter is equal to the length of the given string, then the given string is a palindrome or in other cases, it is not a palindrome
  6. Now, let's come to the second question, here we are going to reverse the given string.
  7. While taking input, we store the given string in a queue and for reversing it, we push it to a new stack.
  8. Now, if we pop the new stack, we get the reversed string.
  9. Note: Please read the program line by line to understand the logic.

Please refer to the comments of the program for more clarity.

#include<bits/stdc++.h>
using namespace std;

int main()
{
    // Initialization of the variables
    cout << "Enter the string: ";
    string s;

    // Taking the string as input
    cin >> s;

    // Declaring stack and queue to check palindrome
    stack<char> s1;
    queue<char> q1;

    // Declaring stack and queue to find the reverse of the given string.
    queue<char> Q;
    stack<char> S;


    // Inserting the input string to the stack and queue
    for(int i=0; i<s.length(); i++)
    {
        s1.push(s[i]);
        q1.push(s[i]);

        Q.push(s[i]);
    }

    // Initializing the boolean to store whether the given string is palindrome or not
    bool isStringPalindrome = false;

    // Checking for the palindrome
    int sameCounter = 0;
    for(int i=0; i< s.length(); i++)
    {
        if(s1.top() == q1.front())
        {
            // Increasing the counter when characters from the starting and from reverse are the same
            sameCounter++;
        }
        s1.pop();
        q1.pop();
    }

    // When all the characters are the same then sameCounter will be equal to the length of the string
    if(sameCounter == s.length())
        isStringPalindrome = true;

    // When isStringPalindrome == 1, it means string is palindrome and when isStringPalindrome == 0, string will not be palindrome
    cout << "Is given string palindrome: ";
    if(isStringPalindrome == 1)
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }

    // Question - 2
    // Reversing the given string
    for(int i=0; i<s.length(); i++)
    {
        S.push(Q.front());
        Q.pop();
    }

    // Finally, printing the reverse of the input string
    cout << "Reverse of the given string: ";
    for(int i=0; i<s.length(); i++)
    {
        cout << S.top();
        S.pop();
    }

    return 0;
}

Sample Input-Output/CodeRun -1:

Sample Input-Output/CodeRun -2:

Repl screenshot:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (3) Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in...
Write one page report about Telecommunicating Services? Make sure that you put a bibliography in the...
Write one page report about Telecommunicating Services? Make sure that you put a bibliography in the paper?
I am down to one last attempt. Please make sure both are correct. 1) Solve the...
I am down to one last attempt. Please make sure both are correct. 1) Solve the given system of differential equations by systematic elimination. (D + 1)x + (D − 1)y = 2 3x + (D + 2)y = −1 (x(t),y(t))=? 2) Solve the given system of differential equations by systematic elimination. D2x − Dy = t (D + 6)x + (D + 6)y = 5 (x(t),y(t))=?
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial...
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial and Transcendental Functions.
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
Show full work: Please make sure to start the comparison with -infinity and NO NOT COUNT...
Show full work: Please make sure to start the comparison with -infinity and NO NOT COUNT SWAPS! Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps)
Options Homework Graph a call option.  Make sure you put the price of the stock on the...
Options Homework Graph a call option.  Make sure you put the price of the stock on the x axis and the change in wealth on the y axis.  There is no such thing as a negative stock price so the y axis does not split the x axis into positive and negative.  Here are the variables to use:  Stock price is currently at $13; stock strike price is $18; the premium is $20.  Make sure you label the change in wealth for the buyer and...
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
Demand and Supply Graph each question separately. Make sure to start with equilibrium and then shift...
Demand and Supply Graph each question separately. Make sure to start with equilibrium and then shift the supply or demand curve to answer the question. Using the information bellow, Draw the Demand and supply Curves for Candy Bars to find the initial equilibrium price and quantity.                                                                                                                        Demand                                            P                           Q                          Pt.                                            $1.25                   1                           A                                            $100                    2                           B                                            $0.75                   3                           C                                            $0.50                   4                           D                                            $0.25                   5                           E                                                           Supply P                                                                      Q                          Pt....
How do I go back to the start of the main method with the given code?...
How do I go back to the start of the main method with the given code? Hello I am making this java program and there is a point in the code where if the user types the character y it will start the program back at the beginning of the main method. How can I do this without drastically changing the code? For instance, if I type y, the program will go back to line 1 and repeat the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT