Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Parenthesis Checker using stack in c++

#include<iostream>
#include<stack>
using namespace std;
bool isPar(char* x,int n)
{
	stack<char> s;
	for (int i = 0; i < n; i++)
	{
		if (x[i] == '(' || x[i] == '{' || x[i] == '[')
			s.push(x[i]);
		if (!s.empty())
		{
			if (x[i] == ')')
			{
				if (s.top() == '(')
				{
					s.pop();
					continue;
				}else
					break;
			}
			//
			if (x[i] == ']')
			{
				if (s.top() == '[')
				{
					s.pop();
					continue;
				}
				else
					break;
			}
			//
			if (x[i] == '}')
			{
				if (s.top() == '{')
				{
					s.pop();
					continue;
				}
				else
					break;
			}
		}
		else
		{
			return false;
		}
	}
	return s.empty() ? true:false;
}
int main()
{
	char x[100];
	cout << "String: " << endl;
	cin >> x;
	if (isPar(x,strlen(x)))
		cout << "Balanced!" << endl;
	else
		cout << "Not Balanced!" << endl;

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to easily trim a str in c++ 
Cpp :: c++ Program for Sum of the digits of a given number 
Cpp :: string to char* 
Cpp :: c++ print string 
Cpp :: increment c++ 
Cpp :: c++ cast char to string 
Cpp :: min element in stl c++ 
Cpp :: index string c++ 
Cpp :: how to get the size of a vector in c++ 
Cpp :: calloc c++ 
Cpp :: c++ get string between two characters 
Cpp :: debugging c/c++ with visual studio code 
Cpp :: how to initialize array with new in c++ 
Cpp :: function c++ 
Cpp :: cpp get last element of vector 
Cpp :: how to remove a index from a string in cpp 
Cpp :: 58. Length of Last Word leetcode solution in c++ 
Cpp :: how to reverse a string in c++ 
Cpp :: union of two arrays leetcode 
Cpp :: string split by space c++ 
Cpp :: implementing split function in c++ 
Cpp :: string length in c++ 
Cpp :: compute power of number 
Cpp :: how can we create 4 digit random number in c++ 
Cpp :: c function as paramter 
Cpp :: cpp array init value 
Cpp :: transformer in nlp 
Cpp :: cpp template 
Cpp :: inheritance example in C plus plus 
Cpp :: abstraction in cpp 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =