balanced parentheses in an expression
class balanced_parentheses_in_an_expression { public static void main(String[] args) { String input= "[()]{}{[()()]()}"; reverse(input); } static String input= "[()]{}{[()()]()}"; static int l=input.length(); static char array[]=new char[l]; static int top=-1; static char stackarray[]=new char[l]; static void reverse(String input) { for (int j = 0; j < l; j++) { char ch = input.charAt(j); if(ch == '{' || ch == '(' || ch == '[') { push(ch); } if(ch == '}' || ch == ')' || ch == ']') { if(top == -1) { System.out.println(" expression is un balanced .."); System.exit(1); } pop(ch); } } if(top!=-1) { System.out.println(" expression is un balanced .."); System.exit(2); } System.out.println(" expression is balanced .."); } public static void push(char value) { top++; stackarray[top]=value; } public static void pop(char ch) { if(top>=0) { if( (ch==')')&&(stackarray[top]=='(') || (ch==']')&&(stackarray[top]=='[') || (ch=='}')&&(stackarray[top]=='{') ) { top--; } else { System.out.println(" expression is un balanced .."); System.exit(1); } } } }
balanced parentheses in an expression
Reviewed by Unknown
on
04:54
Rating:
No comments: