Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
}
Source by codejudge.blogspot.com #
 
PREVIOUS NEXT
Tagged: #Parenthesis #Checker #stack
ADD COMMENT
Topic
Name
7+2 =