Balance parenthesis

import java.util.*; public class Main { public static void compare() {}; public static void insert() {}; public static void main(String[] args) { System.out.println("program for balance parenthesis"); String str = "{{[Ratan]}}"; String str2 = "{{[Ratan]}]}"; char poped; boolean bool=true; Stack mystack = new Stack(); char[] myChar= str2.toCharArray(); for(int i=0;i<myChar.length;i++) { switch (myChar[i]) { case '}': poped = (char)mystack.pop(); if( poped != '{') { System.out.print("not a balance array"); bool=false; } break; case ']': poped = (char)mystack.pop(); if( poped != '[') { System.out.print("not a balance array"); bool=false; } break; case ')': poped = (char)mystack.pop(); if( poped != '(') { System.out.print("not a balance array"); bool=false; } break; case '{': case '[': case '(': //insert(); mystack.push(myChar[i]); break; default: continue; } if(!bool) { break; // breaking the for loop as there is no need to check further strings in characters.. } } if(bool){ System.out.print("Balanced symbol found.."); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.