Created
March 1, 2014 17:15
-
-
Save bitwiser/9293303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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