Answer: C++
Step-by-step explanation:
#include<bits/stdc++.h>
using namespace std;
// function to confirm if paranthesis are balanced
bool areParanthesisBalanced(string expr)
{
stack<char> s;
char x;
// Traversing the Expression
for (int i=0; i<expr.length(); i++)
{
if (expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
// Push the element in the stack
s.push(expr[i]);
continue;
}
// IF current current character does not opening
// bracket, then it must be closing. So stack
// cannot be empty at this point.
if (s.empty())
return false;
switch (expr[i])
{
case ')':
// Reserve the top element in a
x = s.top();
s.pop();
if (x=='' ':
// Reserve the top element in b
x = s.top();
s.pop();
if (x=='(' || x=='[')
return false;
break;
case ']':
// Reserve the top element in c
x = s.top();
s.pop();
if (x =='(' || x == '{')
return false;
break;
}
}
// Check Empty Stack
return (s.empty());
}
// Driver program to test above function
int main()
{
cout<<"Enter the file name\\";
char filename[20];
cin>>filename;
ifstream file; //using input stream to read the contents of the file
file.open(filename);
char ch;
string s="";
while (file >> ch) { //reading the content from file char by char
s=s+ch; //storing the content in the string s
}
if (areParanthesisBalanced(s))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;