Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Created March 1, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwiser/9293303 to your computer and use it in GitHub Desktop.
Save bitwiser/9293303 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
bool checkBalancedParentheses(string s){
stack<char> st;
for(int i=0;i<s.length();i++){
if(s[i]=='('){
st.push(s[i]);
}else if(s[i]==')'){
if(st.empty())
return false;
st.pop();
}
}
if(st.empty())
return true;
else
return false;
}
string processString(string s){
queue<char> q;
string ret = "";
for(int i=0;i<s.length();i++){
if((s[i]>='a' && s[i]<='z')||(s[i]>'A' && s[i]<='Z'))
q.push(s[i]);
}
while(!q.empty()){
ret = ret+q.front();
q.pop();
}
return ret;
}
int main(){
string s;
cout<<"Enter strings(q to quit):\n";
while(getline(cin,s)){
if(s=="q" || s=="Q")
break;
if(checkBalancedParentheses(s)){
cout<<"Parentheses balanced.\n";
}else{
cout<<"Parentheses not balanced.\n";
}
cout<<"Proccesed string: "<<processString(s)<<endl<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment