Question
C++

Problem 4: Enforcing Proper String Delimiters Here, we want to enforce proper delimiter rules in a string. For example, if a user types something like this at the console: This is my string, and it is properly delimited: 1*t 〔3+4) *8). Notice how the open parentheses are properly matching with closed parenthesis. We want to enable a user to create a string using more than one type of delimiter to bracket information into blocks. For example, A string may use braces {), parentheses (), and brackets [] as delimiters. A string is properly delimited if each right delimiter is matched with a preceding left delimiter of the same type in such a way that either the resulting blocks of information are disjoint, or one of them is completely nested within the other. Create a program that utilizes one C++ stack to verify whether or not a string containing any braces, parentheses, and/or brackets is properly formed (delimited). Allow the user to enter the string at the console. Your program will verify if the string is properly delimited. If it is not, inform the user where in the string the violation occurs
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:


//Enforcing Proper String Delimiters
#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to check open parenthesis are properly matching with closed parenthesis.
bool CheckMatch(char first,char last)
{
   if(first == '(' && last == ')') return true;
   else if(first == '{' && last == '}') return true;
   else if(first == '[' && last == ']') return true;
   return false;
}
bool AreProperlyDelimeted(string exp)
{
   stack<char> stack_var;
   for(int i =0;i<exp.length();i++)
   {
       if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
           stack_var.push(exp[i]);
       else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
       {
           if(stack_var.empty() || !CheckMatch(stack_var.top(),exp[i]))
               return false;
           else
               stack_var.pop();
       }
   }
   return stack_var.empty() ? true:false;
}

int main()
{
   /*Code to test String is properly delimeted or not*/
   string A;
   cout<<"Enter a String ";
   cin>>A;
   if(AreProperlyDelimeted(A))
       cout<<"String is properly delimeted\n";
   else
       cout<<"String is not properly delimeted String violation occurred\n";
}

Screenshot:

Add a comment
Know the answer?
Add Answer to:
C++ Here, we want to enforce proper delimiter rules in a string. For example, if 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
  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar...

    Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Write a c++ program ******​IMPORTANT NOTE: DO NOT USE DO NOTTT USE POINTERS,STRING LIBRARY, PRINTF AND...

    Write a c++ program ******​IMPORTANT NOTE: DO NOT USE DO NOTTT USE POINTERS,STRING LIBRARY, PRINTF AND SCANF************* Part A Create a program that does the following: 1 Create a two-dimensional array of characters that has 78 rows, each of which will store, with an initialization list, the name of a PR municipality. Be sure to indicate the number of columns in this array large enough to fit the longest name, considering there must be a space for the ' 0...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

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