Skip to content

Add Check Valid Expression (in C++) #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Check_Valid_Expression.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
#include<stack>
using namespace std;

int length(char *exp){
int length=0;
for(int i=0;exp[i]!='\0';i++)
length++;
return length;
}



bool check_Expression(char *exp){
stack<int> st;
int l =length(exp);
for(int i = 0 ; i < l ; i++){

if(exp[i]=='('||exp[i]=='{'||exp[i]=='['){
st.push(exp[i]);
continue;
}

else if(exp[i]==')'){
if(st.empty() == false){
if(st.top()=='(')
st.pop();
}
else
return false;
}


else if(exp[i]=='}'){
if(st.empty() == false){
if(st.top()=='{')
st.pop();
}
else
return false;
}
else if(exp[i]==']'){
if(st.empty() == false){
if(st.top()=='[')
st.pop();
}
else
return false;
}

}


if(st.empty()==true){
return true;
}
else
return false;
}

int main() {
char input[100000];
cout<<"Enter Expression : ";
cin.getline(input, 100000);
cout<<endl ;
if(check_Expression(input)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}