Question

Write a C++ program that will determine if one input string is a subsequence of a...

Write a C++ program that will determine if one input string is a subsequence of a second input string. In a subsequence, all the characters in the first string will also be in the second string in the same order. For example: “abc” is a subsequence of “qzabc”. While the characters must be in the same order, they do not have to be consecutive. For example: “abc” is also a subsequence of “aaqbzcw”. We will use two stacks one and two. Two determine if the first string is a subsequence of the second string, push each of the charcters from the first input string onto stack one and then push the characters of the second input string onto stack two. Peek at the tops of the two stacks and if they match pop both. If they don’t match pop stack two. If stack one becomes empty, then we have found a subsequence. If stack two becomes

empty and there are still characters on stack one, it is not a subsequence.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING

::::::::::::::::::::::::::::::::::::::::::::::::::: CODE :::::::::::::::::::::::::::::::::::::::::::::::::::

#include <iostream>
using namespace std;

// CLASS FOR STACK
class Stack{
   private:
       string str;   // to store characters
   public:
       // CONSTRUCTOR
       Stack(){
           this->str   = "";   // intialize empty string
       }

       // function to push character
       void push(char c){
           // add character to string
           this->str=this->str+c;
       }

       // function to pop character from stack
       char pop(){
           // if string is empty return zero character
           if(str.length()==0){
               return '\0';
           }

           // if string is not empty
           // store ending character in a variable
           char c=str[str.length()-1];

           // resize string by removing last character
           str.resize(str.size() - 1);

           // return character
           return c;
       }

       // function to return top character
       char peek(){
           // if string is empty return zero character
           if(str.length()==0){
               return '\0';
           }
           // else return last character
           return str[str.length()-1];
       }
};

// MAIN FUNCTION STARTS HERE
int main(){
   string s1,s2;
   // read string 1
   cout << "Enter String 1: ";   cin >> s1;
   // read string 2
   cout << "Enter String 2: ";   cin >> s2;

   // create stacks for string1 and string2
   Stack one;
   Stack two;

   // pushing all the characters of string1 to stack one
   for(int i=0;i<s1.length();i++){
       one.push(s1[i]);
   }
   // pushing all the characters of string1 to stack two
   for(int i=0;i<s2.length();i++){
       two.push(s2[i]);
   }

   // LOOP until either of above stack gets not empty
   while(true){
       // checking stack one is empty or not
       if(one.peek()=='\0'){
           // break if stack one is empty
           break;
       }
       // checking both stack top characters
       if(one.peek()==two.peek()){
           // if both topss are equal
           // pop both stacks character
           one.pop();
           two.pop();
       }
       // if top characters not equal
       else{
           // pop character from stack two
           two.pop();
           // checking stack two is empty or not
           if(two.peek()=='\0'){
               // break if stack two is empty
               break;
           }
       }
   }

   // CHECKING stack one is EMPTY or NOT
   if(one.peek()=='\0'){
       // PRINT message on console IF S1 is substring of S2
       cout << "String1 is Subsequence of String2\n";
   }
   else{
       // PRINT message on console
       // IF S1 is not substring of S2
       cout << "String1 is Not Subsequence of String2\n";
   }
   return 0;
}
// MAIN FUNCTION ENDS HERE

::::::::::::::::::::::::::::::::::::::::::::::::::: OUTPUT :::::::::::::::::::::::::::::::::::::::::::::::::::

- 0 x D : Stack_String.exe Enter String 1: abc Enter String 2: aaqbzcw Stringl is Subsequence of String2, Process exited afte

- O X D : Stack_String.exe Enter String 1: aba Enter String 2: aaqbzcw Stringl is Not Subsequence of String2 Process exited a

::::::::::::::::::::::::::::::::::::::::::::::::::: CODE in EDITOR :::::::::::::::::::::::::::::::::::::::::::::::::::

Stack_String.cpp 1 #include <iostream> 2 using namespace std; // CLASS FOR STACK 59 class Stack{ private: string str; // to s38 39 € 40 // function to return top character char peek() { // if string is empty return zero character if(str.length()==0){if(one.peek()==\0){ // break if stack one is empty break; 0 // checking both stack top characters if(one. peek() ==two.peek

Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.

Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will determine if one input string is a subsequence of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks...

    For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*;    public class GFG {            // This is the temporary stack     static Stack<Integer> res = new Stack<Integer>();     static Stack<Integer> tmpStack = new Stack<Integer>();            //...

  • Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...

    Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example: >>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False >>> Hint: It is not sufficient to just count the number of opening and closing...

  • Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e....

    Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e. your name, then try to compute this string in reverse order using a Stack structure with the Linked List principle. For example: "Nguyen Van A""A Nav Neyugn Exercise 2: 8 points Assume that a queue of N passengers waiting to enter a train car Only the first person in the queue can enter at a time New persons arrive will go to the end...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in...

    JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x onto stack. • pop() -- Removes the element on top of the stack. • top() -- Get the top element. • getMin() -- Retrieve the minimum element in the stack. Example 1: Input ["MinStack", "push", "push","push", "getMin","pop","top", "getMin"] [1], [-2], [0], [-3],[1,1],[1, [1] Output [null, null, null,null,-3, null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);...

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

    //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() {    cout << boolalpha;    cout << "--- stack of int" << endl;    Stack<int> si;    cout << "si intially " << si << endl;   ...

  • Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A...

    Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A user event may be “pressing the Enter key on keyboard” or “clicking a mouse button”. Stack is a data structure with the Last-In-First-Out property (LIFO). If we push the aforesaid user events into a stack, a computer program using that data structure can “rewind” user events by popping them out of stack one at a time. This backtracking feature is available as Edit ->...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT